简体   繁体   English

将 for 循环的结果压入数组

[英]Push into an array the result of for loop

I have an existing array:我有一个现有的数组:

array = [
  { type: 'animal', color: 'green' },
  { type: 'plant', color: 'red' }]

I am making a for loop based on this array.我正在基于这个数组制作一个 for 循环。

for(let i=0; i<array.length; i++){
    //make get request from database
    //first query result
    newObject = {type: 'plant', size: 'small'}

    //second query result
    newObject = {type: 'animal', size: 'large' }
}

If the type of newObject matches that of the array, then, push or append that to array.如果 newObject 的类型与数组的类型匹配,则将其推送或 append 到数组。 This is my desired result:这是我想要的结果:

array = [
  { type: 'animal', color: 'green', size: 'large' },
  { type: 'plant', color: 'red', size: 'small' }]

Try this.试试这个。

 for(let i=0; i<array.length; i++){
        if(array[i].type == 'plant'){
           array[i].size = 'small';
         }
        if(array[i].type == 'animal'){
           array[i].size = 'large';
         }
    }

To check console.log(array);检查console.log(array);

Hope useful.希望有用。

Maybe the best way is doing this:也许最好的方法是这样做:

const idx = array.findIndex(x => x.type === newObject.type );
array[idx] = {...array[idx], ...newObject};

I'd recommend using a data structure that works better for you than an Array .我建议使用比Array更适合您的数据结构。 A Map or an object lets you index data by a key so you don't have to scan through an Array to find something multiple times: Map或 object 可让您通过键索引数据,这样您就不必多次扫描数组来查找内容:

const thingsByType = new Map([
  ['animal', { color: 'green' }],
  ['plant', { color: 'red' }]
]);

thingsByType.get('animal').size = 'small';

// or

const thingsByType = {
  animal: { color: 'green' },
  plant: { color: 'red' }
};

thingsByType.animal.size = 'small';

You can create a Map or Object from an Array if you cannot help starting from that structure:如果您无法从该结构开始,则可以从Array创建MapObject

const thingsByType = new Map(things.map(thing => [thing.type, thing])); 

// or

const thingsByType = things.reduce(
  (result, thing) => Object.assign(result, { [thing.type]: thing }), 
  {});

Just forked from @A.khalifa above.刚刚从上面的@A.khalifa 分叉出来。 This is also another workaround if you don't know exactly what is being fetched from the database.如果您不确切知道从数据库中获取的内容,这也是另一种解决方法。

for(let i=0; i<array.length; i++){
    // make get request from database
    // first query result
    // newObject = {type: 'plant', size: 'small'}
    // -- This is based on the newObject being fetched above
    if(array[i].type == newObject.type){
       array[i].size = newObject.size;
     }
}

Hope this result will be useful.希望这个结果有用。 But one way or another you have to run the loop for each newObject.但是无论如何,您都必须为每个 newObject 运行循环。 Here also 'type' consider as the key as you have to consider fixed one key property.这里也将“类型”视为关键,因为您必须考虑固定的一个关键属性。


array = [
  { type: 'animal', color: 'green' },
  { type: 'plant', color: 'red' }];

let newObject = {type: 'animal', size: 'large' }

for(let i=0; i<array.length; i++){

    if(array[i].type == newObject.type){
      for (let key of Object.keys(newObject)) {
          array[i][key] = newObject[key]

      }
    }

}

console.log(array);

//    array[
//       {type: "animal", color: "green", size: "large"},
//       {type: "plant", color: "red"}
//    ]


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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM