[英]What is the best way to check if an object is an array or not in Javascript?
[英]what's the best way to change an object property in a javascript object array?
是否有最快/更好的執行方式來搜索和更改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.