admin管理员组文章数量:1430460
Say I have an object like so (there could be a lot of items in here);
myObject:{
item1: {
visible: true;
},
item2: {
visible: true;
},
item3: {
visible: false;
},
}
and then an array of objects like so (there could be a lot of items in here);
var objs = [
{ name: 'item1', visible: false },
{ name: 'item2', visible: false },
{ name: 'item3', visible: false }
];
How do I iterate through the object and assign the value of visible to the array only when the names match - If Item1 in the object is visible: false
, then item1 in the array must reflect that
Say I have an object like so (there could be a lot of items in here);
myObject:{
item1: {
visible: true;
},
item2: {
visible: true;
},
item3: {
visible: false;
},
}
and then an array of objects like so (there could be a lot of items in here);
var objs = [
{ name: 'item1', visible: false },
{ name: 'item2', visible: false },
{ name: 'item3', visible: false }
];
How do I iterate through the object and assign the value of visible to the array only when the names match - If Item1 in the object is visible: false
, then item1 in the array must reflect that
- Don't loop through the object. Loop through the array. Find the element by the objs.name, and set the objs.visible equal to the found element's sub-key visible – Taplar Commented Oct 29, 2018 at 22:08
3 Answers
Reset to default 3The most efficient way is to loop through the array as you only iterate the array once.
const myObject = {
item1: {
visible: true
},
item2: {
visible: true
},
item3: {
visible: false
}
};
var objs = [
{ name: 'item1', visible: false },
{ name: 'item2', visible: false },
{ name: 'item3', visible: false }
];
objs.forEach(obj => {
const property = myObject[obj.name];
if (property) {
obj.visible = property.visible;
}
});
console.log(objs);
Loop over your myObject
, and find the object within objs
using find, then set the value of that found object:
for (let o in myObject) {
let obj = objs.find(i => i.name == o)
if(!obj) continue
obj.visible = myObject[o].visible
}
Here is a working example:
const myObject = {
item1: {
visible: true
},
item2: {
visible: true
},
item3: {
visible: false
}
}
var objs = [
{ name: 'item1', visible: false },
{ name: 'item2', visible: false },
{ name: 'item3', visible: false }
]
for (let o in myObject) {
let obj = objs.find(i => i.name == o)
if(!obj) continue
obj.visible = myObject[o].visible
}
console.log(objs)
const myObject = {
item1: {
visible: true
},
item2: {
visible: true
},
item3: {
visible: false
}
};
var objs = [
{ name: 'item1', visible: false },
{ name: 'item2', visible: false },
{ name: 'item3', visible: false }
];
Object.getOwnPropertyNames(myObject).forEach(propertyName => {
let obj = objs.find(o => o.name === propertyName);
if (obj) {
obj.visible = myObject[propertyName].visible;
}
});
console.log(objs);
本文标签:
版权声明:本文标题:Loop over an object and assign a value to an array of objects if certain conditions are met - vanilla JavaScript - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745553740a2663060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论