简体   繁体   English

js中修改对象数组里面的对象数组

[英]Modify an array of objects inside an array of objects in js

Hello developers I'm trying to modify an array of objects inside an array of objects before deploying its result to Redux reducer.开发人员您好,我试图在将结果部署到 Redux reducer 之前修改对象数组中的对象数组。 The array is obtained through a request to an endpoint, reason why i must to create an instance of writable copy of it, and then proceed on the process该数组是通过对端点的请求获得的,为什么我必须创建它的可写副本的实例,然后继续处理

Lest say i have this array:免得说我有这个数组:

 allProducts=    [
            {
               
                "product_type": "Bikes",
                "product_imgs": [
                    {
                        "id": 5,
                        "url": "Mountain Bike/Screenshot (200)"
                    },
                    {
                        "id": 6,
                        "url": "Mountain Bike/Screenshot (200)"
                    }
                ],
                "product_name": "product test 1"
            },
            {
               
                "product_type": "Bikes",
                "product_imgs": [
                    {
                        "id": 7,
                        "url": "City Bike/banderaa"
                    },
                    {
                        "id": 8,
                        "url": "City Bike/banderaa"
                    }
                ],
                "product_name": "product test 2"
            }
       ]

I would like to modify the items inside the array product_imgs of each object, but for that, having in mind this array comes from a request, i do create a readable copy an over that i set the logic.我想修改每个 object 的数组 product_imgs 内的项目,但为此,考虑到这个数组来自请求,我确实创建了一个可读的副本并覆盖了我设置的逻辑。

let instance=[...allProducts];

then using a double for each (though i also tried using a doule for loop) i reach till every image inside the array of objects product_imgs of each object:然后为每个使用一个双精度(虽然我也尝试使用一个双精度循环)我到达每个 object 的对象 product_imgs 数组中的每个图像:

        instance.forEach(array=>array.product_imgs.map(element => {
          
          this.imgDownLoaderFirebase
          .ref(element.url)
          .getDownloadURL()
          .toPromise()
          .then((url) => {
            console.log(url);
            
            //then in this space once the url of some firebase endpoint is reached and else 
            //i would like to modify that object inside the array product_imgs which is at the same time 
            //part of the instance array.
            //For that i expose that this new url gotten would be asigned as the new 
            //value thus
            
            element = { ...element };
            element.url=url
            
            console.log(element);
            console.log(instance);//Printing the general array in order to check if changes committed
           
          })
       })

        

I want to specify that i use first a foreach and then a map in order to modify the inner array of objects result, but using a double for each doesn't precisely inmprove this situation:我想指定我首先使用 foreach 然后使用 map 来修改对象结果的内部数组,但是对每个对象使用双精度并不能完全改善这种情况:

instance.forEach(array=>array.product_imgs.forEach(element => {........

Then checking the logs, the element (item url) inside the array of objects product_imgs of the array of obejcts instance, is modified, but the external array containing the inner modified not然后查看日志,objects实例数组product_imgs对象数组里面的元素(item url)被修改了,但是包含内部修改的外部数组没有在此处输入图像描述

How could i improve this?我该如何改进呢? Thanks谢谢

If your goal is to extract all product_img values from your array, you could try something like the following:如果您的目标是从数组中提取所有 product_img 值,您可以尝试如下操作:

// This line will convert your array of object into an array of array of urls, using a destructuring process
const urls = allProducts.map(({ product_img }) => product_img);

// This line will merge the previous result into a single-level array of urls that you can iterate onto.
const result = [].concat([], ...res);

Edit: I forgot to mention that this process will in fact return an array of objects including your id and url.编辑:我忘了说这个过程实际上会返回一个对象数组,包括你的 id 和 url。

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

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