[英]How to loop through array containing objects and array of objects in JS
I have the following data:我有以下数据:
const store = [{
name: "outside",
color: "lightpink",
position: [-400, 0, 300],
url: "src/assets/picture1.jpg",
link: 1,
},
[{
name: "To the Yard",
color: "lightblue",
position: [-100, 0, 900],
url: "src/assets/picture5.jpg",
link: 2,
},
{
name: "To the Room",
color: "lightblue",
position: [-100, 0, 900],
url: "src/assets/picture5.jpg",
link: 0,
},
],
{
name: "Hall",
color: "red",
position: [20, 10, 0],
url: "src/assets/picture3.jpg",
link: 1,
},
// ...
];
And what I want to achieve is to put all link property data into new array by using map() function in JS.而我想要实现的是通过使用 JS 中的 map() 函数将所有链接属性数据放入新数组中。 The problem is I also have array of objects inside array so I need to put 2 and 0 along with 1, 1. So the result should be [1, 2, 0, 1].
问题是我在数组中也有对象数组,所以我需要将 2 和 0 与 1, 1 一起放入。所以结果应该是 [1, 2, 0, 1]。 To achieve that I tried this:
为了实现这一点,我尝试了这个:
store.map((entry) => {
if (Array.isArray(entry)) {
return entry.map(item => item.url)
} else {
return entry.url
}
})
but no result但没有结果
You could take Array#flatMap
with a recursive mapping for arrays.您可以将
Array#flatMap
与数组的递归映射一起使用。
const getLink = o => Array.isArray(o) ? o.flatMap(getLink) : o.link, store = [{ name: "outside", color: "lightpink", position: [-400, 0, 300], url: "src/assets/picture1.jpg", link: 1 }, [{ name: "To the Yard", color: "lightblue", position: [-100, 0, 900], url: "src/assets/picture5.jpg", link: 2 }, { name: "To the Room", color: "lightblue", position: [-100, 0, 900], url: "src/assets/picture5.jpg", link: 0 }], { name: "Hall", color: "red", position: [20, 10, 0], url: "src/assets/picture3.jpg", link: 1 }], result = store.flatMap(getLink); console.log(result);
You could .flat
( Array.prototype.flat() ) the array then .map
你可以
.flat
( Array.prototype.flat() ) 数组然后.map
const res = store.flat().map((s) => s.link)
const store = [ { name: "outside", color: "lightpink", position: [-400, 0, 300], url: "src/assets/picture1.jpg", link: 1, }, [ { name: "To the Yard", color: "lightblue", position: [-100, 0, 900], url: "src/assets/picture5.jpg", link: 2, }, { name: "To the Room", color: "lightblue", position: [-100, 0, 900], url: "src/assets/picture5.jpg", link: 0, }, ], { name: "Hall", color: "red", position: [20, 10, 0], url: "src/assets/picture3.jpg", link: 1, }, // ... ] const res = store.flat().map((s) => s.link) console.log(res)
You can flatten the array and then call map()
function.您可以展平数组,然后调用
map()
函数。
const store = [ { name: "outside", color: "lightpink", position: [-400, 0, 300], url: "src/assets/picture1.jpg", link: 1, }, [ { name: "To the Yard", color: "lightblue", position: [-100, 0, 900], url: "src/assets/picture5.jpg", link: 2, }, { name: "To the Room", color: "lightblue", position: [-100, 0, 900], url: "src/assets/picture5.jpg", link: 0, }, ], { name: "Hall", color: "red", position: [20, 10, 0], url: "src/assets/picture3.jpg", link: 1, }, ]; let result = store.flat().map(e => e.url); console.log(result);
flat
你的阵列,然后映射展平的阵列。
store.flat().map(s => s.url)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.