How to loop through array with arrays/objects and assign properties new values in Angular?

I have an array of arrays and objects. I have a function whereI want to assign a value to a property (so for example ‘call’:” of $scope.companies[0].users becomes whatever value the user checks in the checkbox). I researched it and I just don’t know how to do that and everything I did so far is wrong. Thanks a lot!!

      <form action="" ng-click="change(key)">
        <input ng-model="key.call"type="checkbox"">Call
        <br>
        <input ng-model="key.person"type="checkbox" >Person
        <br>
        <input type="checkbox"ng-model="key.dial">Dial
        <br>
        <input type="checkbox" ng-model="key.voice">Voice          
    </form>

app.controller('appCtrl', function($scope) {
    $scope.companies = [{
        name: 'The Best Company Denim',
        users: [{
            firstName: 'Alex',
            lastName: 'D',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Sarah',
            lastName: 't',
            number: 14,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'J',
            lastName: 'd',
            number: 07,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }, {
        name: 'The Best Company Elegant',
        users: [{
            firstName: 'Alx',
            lastName: 'B',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Seth',
            lastName: 'w',
            number: 12,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'J.S',
            lastName: 'B',
            number: 7.
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }, {
        name: 'The Best Company by Julia',
        users: [{
            firstName: 'Aleddddx',
            lastName: 'l',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Maggy',
            lastName: 'n',
            number: 1,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Ja',
            lastName: 'Key',
            number: 123,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }]

    $scope.change = function(key) {
        for (var i = 0; i < $scope.companies[0].users; i++) {
            $scope.companies[0].users[i].call: key)
    }
}
});


Source: stackoverflow-javascript

String to Object with JavaScript

I would like to know the most efficient way to convert this string into an object in JavaScript.

This is the ugly string:

  ""date","initRegistrations","registrations","finalizeRegistrations"rn"03/29/2017","0","0","0"rn"03/30/2017","5","4","4"rn"03/31/2017","42","0","0"rn"04/01/2017","192","0","0"rn"04/02/2017","4","0","0"rn"04/03/2017","23","0","0"rn"04/04/2017","5","0","0"rn"

At the moment I’m using this function, but it does not work fine, the expected output would be a object of object with the keys date, initRegistrations, registrations, finalizeRegistrations:

   function convertToArrayOfObjects(data) {
    var keys = data.shift(),
        i = 0, k = 0,
        obj = null,
        output = [];

    for (i = 0; i < data.length; i++) {
        obj = {};

        for (k = 0; k < keys.length; k++) {
          if(data[i][k]){
            obj[keys[k]] = data[i][k];
          }

        }

        output.push(obj);
    }

    return output;
}

Is there any fancy way to achieve this easily or I should go playing with my array of objects?
Any help or advice is appreciated.
Thanks.


Source: stackoverflow-javascript

Can’t access object property in Laravel

I started learning Laravel since yesterday and face some difficulties to get property of an object.

Here is the class (created with artisan) :

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Task extends Model
{
    public $description;
    public $completed;
    public $timestamps = false;

    public function isComplete()
    {
        return $this->completed;
    }
}

And here is the issue :

>>> $tasks = AppTask::all();
=> IlluminateDatabaseEloquentCollection {#654
     all: [
       AppTask {#652
         id: "1",
         description: "Learn Laravel",
         completed: "0",
       },
     ],
   }
>>> $tasks[0]->id
=> 1
>>> $tasks[0]->description
=> null

Why can I access the id but not the description ?


Source: stackoverflow-php

How to change id to value in object.

Have an object that I received from server. in that look

 Object
    id:1
    name: "school_test_1"

 Object
    id:2
    name:"school_test_2

I need to change all “id” to name “value”
Try to parse to JSON change it and convert to the array, but have very bad results

var x = nextProps.campus;
        var fff =  JSON.stringify(x);

        var res = fff.replace(/name/g, "value");

        var arr = [];
        for (var prop in res) {
            arr.push(res[prop]);
            }


Source: stackoverflow-javascript

Splicing properties from an object?

Is there a utility (jquery, lodash, etc) that extracts properties from an object to another object and then deletes them from the original object? Similar to Array’s splice?

obj1 = {color:"red", age:"23", name:"cindy"}
obj2 = extract(obj1, ["color","name"])

result:

obj1 is { age:23 }
obj2 is {color:"red", name:"cindy"}


Source: stackoverflow-javascript

Trying to recreate my concole.log objects

I have a var that contains 4 objects that I see when I log it out in the console. But I have been trying to create the structure of this return variable (I want to sort it but first want to recreate the structure), but are failing at the last hurdle.

This is what the console puts out on original variable: [Object, Object, Object, Object]

This is what I get with created variable: [Object]

Here is my code to try and re-create it:

this.obj =      [{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                             {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
                     color:'blue'}],
                    [{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                             {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
                     color:'green'}],
                    [{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                             {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
                     color:'red'}],
                    [{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                             {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
                     color:'yellow'}]
                    ;

The first objects of both original and created are identical, but why does it not see my other three objects? If I add two [] around my code, it’s sees all four, but then they have changed to arrays instead of objects…


Source: stackoverflow-javascript