2

Am trying to loop over an object in angular-js and add an array to object foreach looped object , my code is as below but doesn't seem to work

vm.patients= [
        {[ {"p1": "value1"} ]},
        {[ {"p1": "value1"} ]},
        {[ {"p1": "value1"} ]}

    }];

for(var i=0; i < vm.patients.length; i++){
vm.patients[i].concat([{"z1": "z2",}]);`
}

My aim is to add {"z1": "z2",} to each loop ie the 3 loops arrays so that i end with below

vm.patients= [
            {[ {"p1": "value1"},{"z1": "z2",} ]},
            {[ {"p1": "value1"},{"z1": "z2",} ]},
            {[ {"p1": "value1"},{"z1": "z2",} ]}

        }];

concat however doesn't seem to work , any solution ?

12
  • vm.patients is not an array. you need to use brackets [ ] to define an array Commented Dec 11, 2015 at 14:58
  • you concat an object to string what did you expect? Commented Dec 11, 2015 at 14:58
  • 1
    patients doesn't have a length property Commented Dec 11, 2015 at 14:59
  • Patients is an object not an array. If you wish to itterate it you should use a for in loop. However the order of properties iterated cannot be predicted or guaranteed. Commented Dec 11, 2015 at 15:00
  • Are you aware you have an strange accent mark there? ...({"z1": "z2",});` Commented Dec 11, 2015 at 15:03

4 Answers 4

6

You don't iterate your objects using a for(;;) loop, if they're not array-like objects.

You can iterate with for(in), however:

for(var key in yourObj) {
    if (obj.hasOwnProperty(key)) {
        var value = yourObj[key];
        //your code here
    }
}

Or angularjs-fashion:

angular.forEach(yourObj, function(value, key) {
    // your code here
})

Additionally:

vm.patients[i].concat([{"z1": "z2",}]);

is wrong, since you are calling something like "patient1".concat([{"z1": "z2"}]) which:

  1. Does not alter the value. Try executing in the console the following code:

     var x = "a";
     x.concat("b");
     console.log(x);
    
  2. Sounds to be wrong. Try executing the following in the console:

     var x = "a";
     console.log(x.concat([{'a':'b'}]))
    

    and see what happens.

Tip: If you have troubles with your code, try separating all the code in single instructions and assignments.

Perhaps you intended:

var yourObj = {
    "p1": {"name": "patient1"},
    ... others ...
};
angular.forEach(yourObj, function(value, key) {
    // your code here
    angular.extend(value, {"z1": "z2"})
})
Sign up to request clarification or add additional context in comments.

1 Comment

while the question is unclear, the answer remains unclear.
1

Is this what you are trying to accomplish?

var vm = {};
vm.patients = [{
        "p1": "value1",
        "p2": "value2",
        "p3": "value3"
    }];

for(var i=0; i < vm.patients.length; i++){
    var p = vm.patients[i];
    p["z1"] = "z2";
}
// The result here is that each element in the array will have an
// addition attribute named z1 with the value of "z2".

You had some syntax issues with your JavaScript, but this code is cleaner.

I made patients into an array and modified your other code that affects change on the items in the array.

1 Comment

this looks promising let me test this !
1

concat is an Array function, you can not use it over an object. secondly, if you want to use concat, second parameter also should be an array : [1].concat([2,4]). third, you are trying to concat something to a string which is can't be concat with an object. Then, If you want to iterate over the object and extend it, you should do :

Object.keys(obj).forEach(function(ob) {
  obj[ob] = Object.assign(obj[ob], {newParam1: 1, newParam2: 2});
});

but you code in angular this is more appropriate :

angular.forEach(obj, function(v) {
  v = angular.extend(v, {newValue1: 1, newValue2: 2});
});

2 Comments

what does v stand for ?
object instance in your array
1

Assuming you have a data structure like

vm.patients = [{ p1: "value1" }, { p1: "value1" }, { p1: "value1" }];

then this would work:

var vm = {};
vm.patients = [{ p1: "value1" }, { p1: "value1" }, { p1: "value1" }];

vm.patients.forEach(function (a) {
    a.z1 = 'z2';
});

document.write('<pre>' + JSON.stringify(vm.patients, 0, 4) + '</pre>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.