简体   繁体   English

如何遍历 javascript 中数组内的嵌套 object

[英]how to iterate through nested object which is inside an array in javascript

I want to iterate through imageUrl and keep each element inside li tag.我想遍历imageUrl并将每个元素保留在 li 标记内。 I'm fetching only one element based on some condition, in the array then I want to iterate through all the images my array我根据某种条件只获取数组中的一个元素,然后我想遍历数组中的所有图像

const productsArr = [
    {
      id: "p1",
      title: "Colors",
      price: 100,
      imageUrl: {
        imageUrl1:
          "img/Album%201.png",
        imageUrl2:
          "img/Album%202.png",
        imageUrl3:
          "img/Album%203.png",
        imageUrl4:
          "img/Album%204.png",
    },
    },

    {
      id: "p2",
      title: "Black and white Colors",
      price: 50,
      imageUrl: {
        imageUrl4:
          "img/Album%205.png",
        imageUrl1:
          "img/Album%206.png",
        imageUrl2:
          "img/Album%207.png",
        imageUrl3:
          "img/Album%208.png",
      },
    },]

code to iterate through the array.遍历数组的代码。 I think I should not use map here but I could not come up with the solution.我想我不应该在这里使用 map 但我想不出解决方案。

console.log(product);
 const productImage= product[0].imageUrl.map(element => {
   return <li>{<img src={element} alt={product[0].title}/>}</li>
 });

just iterate through array using array methods and call the nested objects using dot natation只需使用数组方法遍历数组并使用点 natation 调用嵌套对象

for example:例如:

productsArr.map(x =>{
  x.imageUrl.imageUrl1)

You can loop through the productsArr and then use Object.values to create an array of values from imageUrl object and iterate it through it to render li with image.您可以循环遍历productsArr ,然后使用Object.valuesimageUrl object 创建一个值数组,并遍历它以使用图像渲染li

productsArr.map((item)=>{
  return Object.values(item.imageUrl).map((imgUrl)=> <li><img src={imgUrl} alt={item.title}/></li>)
}) 

Try this :试试这个

 class TodoApp extends React.Component { constructor(props) { super(props) this.productsArr = [ { id: "p1", title: "Colors", price: 100, imageUrl: { imageUrl1: "img/Album%201.png", imageUrl2: "img/Album%202.png", imageUrl3: "img/Album%203.png", imageUrl4: "img/Album%204.png", } }, { id: "p2", title: "Black and white Colors", price: 50, imageUrl: { imageUrl4: "img/Album%205.png", imageUrl1: "img/Album%206.png", imageUrl2: "img/Album%207.png", imageUrl3: "img/Album%208.png", } }] } render() { return ( <div> {this.productsArr.map((obj) => { return Object.keys(obj.imageUrl).map((item) => ( <li>{ obj.imageUrl[item] }</li> )); })} </div> ) } } ReactDOM.render(<TodoApp />, document.querySelector("#app"))
 #app { padding: 20px; } li { margin: 8px 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

const product = this.productsArr.map((item) => {
  return Object.entries(item.imageUrl).map((key) => {
    return {
      imgUrl : key[1]
    };
  });
});
 console.log(product[0]);
// this will give array of imgURL of product[0]

try this out试试这个

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

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