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

Share Improve this question asked Oct 29, 2018 at 22:06 DonegalBoyVancouverDonegalBoyVancouver 1212 silver badges10 bronze badges 1
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

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

本文标签: