简体   繁体   English

遍历数组,将每个项目添加到对象中,然后将其推入Javascript中的数组

[英]Loop through an Array, add each item to an object and push it to an array in Javascript

I have an array of ID's as below: 我有一个ID数组,如下所示:

[121, 432, 322]

I want all of it to be added to an array in the following format (Expected Output) : 我希望将其全部以以下格式(预期输出)添加到数组中:

[
    {
        "term": {
            "brand_id": 121
        }
    },
    {
        "term": {
            "brand_id": 432
        }
    },
    {
        "term": {
            "brand_id": 322
        }
    }
]

I am able to get the structure right and get a result almost as expected. 我能够正确地构造结构并获得几乎预期的结果。 But am ending up having just the last value as the value in all items of the object as below (Current Output) : 但是最终将最后一个值作为对象的所有项中的值,如下所示(当前输出)

[
        {
            "term": {
                "brand_id": 322
            }
        },
        {
            "term": {
                "brand_id": 322
            }
        },
        {
            "term": {
                "brand_id": 322
            }
        }
    ]

My code is as below: 我的代码如下:

The array of IDs is in an array named brands. ID数组位于名为brands的数组中。

let brands_formated = [];
//I have the array stored in `brands`
let format =  { "term" : {
                      "brand_id": 0 //will be replaced
                     }
              };

brands.forEach(function(brand) {
    //The structure for brand query
    format.term.brand_id = brand;
    //Check if the right brand is format. Outputs as desired.
    console.log(format);                            
    brands_formated.push(format);

});

Though console.log in loop confirms am iterating correctly. 尽管console.log in循环确认正确迭代。 The final output just has one value. 最终输出只有一个值。

You currently only have one variable for format - you're only ever pushing one item to the array, you're just mutating it multiple times, resulting in the array containing 3 references to the same object. 当前,您只有一个用于format变量-您只将一项推入数组,只是对其进行了多次变异,导致数组包含对同一对象的3个引用。

Create the format on each iteration instead. 而是在每次迭代时创建format .map is more appropriate than .forEach when transforming one array into another: 当将一个数组转换为另一个数组时, .map.forEach更合适:

 const input = [121, 432, 322]; console.log( input.map(brand_id => ({ term: { brand_id }})) ); 

While this answer explained the problem of your code. 虽然答案说明了您的代码问题。 Here is my way to solve the same problem. 这是我解决相同问题的方法。

const input = [121, 432, 322];
Array.from(input, brand_id => ({ term: { brand_id }}))

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

相关问题 遍历数组并将其推入数组 object javascript - Loop through an array and push it into array of object javascript 循环遍历 json 数组并使用 javascript 添加到每个对象 - loop through json array and add to each object using javascript 遍历 object 并将其推入新数组 JavaScript - Loop through object and push it into new array JavaScript 遍历 object 中的数组并推送到 Javascript 中的新数组 - Loop through array within an object and push to a new array in Javascript Javascript - 如何遍历每个单词并将它们推送到一个空数组 - Javascript - How to loop through each word and push them to an empty array JavaScript / jQuery:将项目推送到每个项目具有相同键的数组/对象 - JavaScript / jQuery: Push items to array / object with same key for each item Angular NgFor遍历数组对象,每个项目都有延迟 - Angular NgFor Loop Through Array Object with Delay for Each Item 如何在Clojurescript中循环JavaScript对象并将每个对象推送到一个数组中 - How to loop a JavaScript object and push each into an array, in Clojurescript 如何通过Javascript $ .each中的循环将对象添加到数组中? - How to add objects into an array through loop in Javascript $.each? 有没有办法在JavaScript中为数组的每个对象添加相同的项目val =“4”? - Is there a way to add the same item val=“4” to each object of array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM