简体   繁体   English

从对象数组中收集数据并将它们放入新的对象数组中

[英]collecting data from an array of objects & put them in a new array of objects

I have an array of objects Called Lines.我有一个名为 Lines 的对象数组。 The Lines array is inside rows array & the rows array is inside tabs array. Lines数组位于rows数组内,而 rows 数组位于tabs数组内。 I want to take some data from lines array & put them in another array called colors我想从行数组中获取一些数据并将它们放入另一个名为colors的数组中

the array look like this----数组看起来像这样----

 "tabs": [{ "selectedHouseType": "1", "rows": [{ "selectedDecor": "2", "lines": [{ "selectedColor": "white", "selectedQty": 0, "selectedUnit": "1", }, { "selectedColor": "black", "selectedQty": "2", "selectedUnit": "3", }] }, { "selectedDecor": "1", "lines": [{ "selectedColor": "black", "selectedQty": 0, "selectedUnit": "2", " }] }] }, { "selectedHouseType": "select", "rows": [{ "selectedDecor": "2", "lines": [{ "selectedColor": "red", "selectedQty": 0, "selectedUnit": "", }] }] }]

I want to collect the datas from lines array & put them in another array called " colors "我想从lines数组中收集数据并将它们放入另一个名为“ colors ”的数组中

which will look like this---看起来像这样---

colors:  [{
          "selectedColor": "white",
          "selectedQty": 0,
          "selectedUnit": "1",

        }, {
          "selectedColor": "black",
          "selectedQty": "2",
          "selectedUnit": "3",
        },
        {
          "selectedColor": "black",
          "selectedQty": 0,
          "selectedUnit": "2",
        },
        {
          "selectedColor": "red",
          "selectedQty": 0,
          "selectedUnit": "",
        }
]

I am using vue.js .我正在使用vue.js How do I do this?我该怎么做呢?

Try this:尝试这个:

const colors = tabs.reduce((acc, tab) => {
    const rows = tab.rows
    const lines = rows.reduce((acc, row) => {
        const lines = row.lines
        return [...acc, ...lines]
    }, [])
    return [...acc, ...lines]
}, [])

you can do something like this using flatMap你可以使用flatMap做这样的事情

 const data = { "tabs":[ { "selectedHouseType":"1", "rows":[ { "selectedDecor":"2", "lines":[ { "selectedColor":"white", "selectedQty":0, "selectedUnit":"1" }, { "selectedColor":"black", "selectedQty":"2", "selectedUnit":"3" } ] } ] }, { "selectedHouseType":"select", "rows":[ { "selectedDecor":"2", "lines":[ { "selectedColor":"red", "selectedQty":0, "selectedUnit":"" } ] } ] } ] } const lineColors = data.tabs.flatMap(t => t.rows.flatMap(r => r.lines)) console.log(lineColors)

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

相关问题 从对象数组中获取匹配的属性并将它们放入新数组中 - Get matching properties out of array of objects and put them in a new array 从数组中获取具有唯一ID的第一个对象,并将其放入新列表中 - Take the first objects with unique IDs from an array and put them in new list 根据过滤后的对象数组中的数据创建新的对象数组 - Create new array of objects based on data from a filtered array of objects 如何从数组中提取对象以自动将它们放入变量中? - How to extract the objects from an array to put them in variables automatically? 如何将对象数组中的值放入JavaScript中的新数组中 - How to put values from an array of objects into a new array in JavaScript 函数中的对象的垃圾收集 - Garbage Collecting array of objects in a function 从对象数组中收集键并将其缩小为单个数组并删除重复项 - Collecting the keys from array of objects and reducing it into a single array and removing duplicates 如何将对象数组放入新的Json对象 - How to put array of objects into new Json object 数组拆分为 2 和 map 对应的对象并将它们放入结果数组中 - Array split into 2 and map the corresponding objects and put them in the resultant array 从Array对象中获取关键数据,并将其转换为一个分隔的字符串 - Taking key data from objects in Array and turning them into a , separated string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM