简体   繁体   中英

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. 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]. 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.

 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

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.

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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