简体   繁体   English

更改javascript对象数组中的对象属性的最佳方法是什么?

[英]what's the best way to change an object property in a javascript object array?

are there fastest/better performing ways to search and change a property of a javascript array of objects? 是否有最快/更好的执行方式来搜索和更改javascript对象数组的属性?

var myObjArr = [{'item' : 'one' , 'myProp' : 'propOne' }, 
                {'item' : 'two' , 'myProp' : 'propTwo' }];

for (var i = 0; i < myObjArr.length; i++) { 

    var myObj = myObjArr[i]; 

    for(prop in myObj){ 

        if(prop === 'item' && myObj[prop] == 'two'){

            myObj.myProp = 'propTwoEdited';

        }

   }        

} 
//forEach is faster
myObjArr.forEach(function(obj) {

    //just test the item prop value equals 'two' or not
    if (obj.item == 'two') {

        //obj is object, and a reference
        obj.myProp = 'propTwoEdited';
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中检查对象是否为数组的最佳方法是什么? - What is the best way to check if an object is an array or not in Javascript? 更改属性值的最佳方法。 它在对象数组的对象内 - Best way to change a value of a property. It's within an object of an array of objects PixiJS 更改 Graphics 对象颜色的最佳方法是什么? - PixiJS what's the best way to change a Graphics object's colour? 将对象属性链接到值的最佳方法是什么? - What's the best way to keep an object property linked to a value? 用属性检查数组对象是否存在的最佳方法是什么? - What is the best way to check for existence of array object with property? Javascript Array to Object最佳方法 - Javascript Array to Object best way 在javascript对象中,获取值属性的最佳方法是什么? - in a javascript object, what's best way to get the attribute of a value? 在JavaScript中不变地更新对象某些属性的最佳方法是什么? - What's the best way to update some properties of an object immutably in JavaScript? 从对象数组中查找对象索引的最佳方法是什么 - javascript - What is the best way to find index of object from array of objects - javascript 在 Javascript 对象中重命名所有属性名称的最快方法是什么? - What's the quickest way to rename all property names in a Javascript object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM