最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to loop over angular js object , and add object to the looped - Stack Overflow

programmeradmin0浏览0评论

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 ?

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 ?

Share Improve this question edited Dec 11, 2015 at 15:17 Jimmy Obonyo Abor asked Dec 11, 2015 at 14:56 Jimmy Obonyo AborJimmy Obonyo Abor 7,89510 gold badges48 silver badges71 bronze badges 12
  • vm.patients is not an array. you need to use brackets [ ] to define an array – Glenn Ferrie Commented Dec 11, 2015 at 14:58
  • you concat an object to string what did you expect? – Poyraz Yilmaz Commented Dec 11, 2015 at 14:58
  • 1 patients doesn't have a length property – Ramanlfc 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. – ste2425 Commented Dec 11, 2015 at 15:00
  • Are you aware you have an strange accent mark there? ...({"z1": "z2",});` – codebear22 Commented Dec 11, 2015 at 15:03
 |  Show 7 more ments

4 Answers 4

Reset to default 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"})
})

Is this what you are trying to acplish?

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.

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});
});

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>');

发布评论

评论列表(0)

  1. 暂无评论