【问题标题】:find an object in Array of Array在 Array of Array 中查找对象
【发布时间】:2020-09-14 16:33:25
【问题描述】:

如果我想要一个示例循环通过此数组在 Items Array 中查找特定项目如何处理?我做了这个逻辑,但它不起作用

DATA.map((D)=>{
    return D.items.find((item)=>{
       return item.name ==='Blue Beanie'
    })
  })

这是数组加上如何创建新的 ONE 数组包括两个项数组,如下所示: items: [{ 编号:1, 名称:'棕色边缘', 价格:25 }, { 编号:2, 名称:“蓝色豆豆”, 价格:18 }, { 编号:3, 名称:“阿迪达斯 NMD”, 价格:220 }, { 编号:4, 名称:“阿迪达斯 Yeezy”, 价格:280 } ]

const DATA= [
  {
    id: 1,
    title: 'Hats',
    routeName: 'hats',
    items: [
      {
        id: 1,
        name: 'Brown Brim',
        price: 25
      },
      {
        id: 2,
        name: 'Blue Beanie',
        price: 18
      }
    ]
  },
  {
    id: 2,
    title: 'Sneakers',
    routeName: 'sneakers',
    items: [
      {
        id: 3,
        name: 'Adidas NMD',
        price: 220
      },
      {
        id: 4,
        name: 'Adidas Yeezy',
        price: 280
      }
             ]
  }
];

【问题讨论】:

  • 您的预期输出是什么?
  • 一个对象,其名称 ==='Blue Beanie' :{ id: 2, name: 'Blue Beanie', price: 18 }

标签: javascript arrays reactjs object


【解决方案1】:

DATA 转换为项目列表并从该列表中找到您期望的项目

const res = DATA.flatMap((D) => D.items).find(
  (item) => item.name === "Brown Brim"
)

const DATA = [
  {
    id: 1,
    title: "Hats",
    routeName: "hats",
    items: [
      {
        id: 1,
        name: "Brown Brim",
        price: 25,
      },
      {
        id: 2,
        name: "Blue Beanie",
        price: 18,
      },
    ],
  },
  {
    id: 2,
    title: "Sneakers",
    routeName: "sneakers",
    items: [
      {
        id: 3,
        name: "Adidas NMD",
        price: 220,
      },
      {
        id: 4,
        name: "Adidas Yeezy",
        price: 280,
      },
    ],
  },
]

const res = DATA.flatMap((D) => D.items).find(
  (item) => item.name === "Brown Brim"
)

console.log(res)

参考

Array.prototype.flatMap()

【讨论】:

    【解决方案2】:

    也许这有帮助?

    const DATA= [
      {id: 1,title:'Hats',routeName:'hats',
       items:[{id: 1,name:"Brown Brim",price:25},
              {id: 2,name: 'Blue Beanie',price: 18}]},
      {id: 2,title: 'Sneakers',routeName: 'sneakers',
       items: [{id: 3,name: 'Adidas NMD',price: 220},
               {id: 4,name: 'Adidas Yeezy',price: 280}]}
    ];
    console.log(DATA.map(D=>D.items.find(item=>item.name==='Brown Brim'))
                    .filter(e=>e))

    .map 返回与您的条件匹配的元素或undefined,链接的.filter 然后删除所有“虚假”元素,即。 e.所有undefined 的。

    【讨论】:

      【解决方案3】:

      至于第一个问题“loop Through this Array to find a specific item in Items Array” 鉴于它没有以任何方式排序,这可以通过遍历 DATA 数组并在项目内搜索来完成 如果想从“forEach”范围之外访问项目,则必须在外部声明变量

      关于第二个问题,在迭代数组时使用reduce函数

      注意:您显然可以将这两个任务组合起来,因为您已经遍历了数组,因此无需重复执行两次。但为了避免混淆,我把逻辑分开了。 此外,如果您确实选择组合任务,则使用 reduce 无关紧要,但与第一个问题的答案非常相似,您可以声明一个缓冲区(如数组),然后随时将项目复制到其中(我'将省略有关性能的问题)

      const DATA = [
        {
          id: 1, title: 'Hats', routeName: 'hats',
          items: [
            {id: 1,name: 'Brown Brim',price: 25},
            {id: 2,name: 'Blue Beanie',price: 18}
          ]
        },
        {
          id: 2, title: 'Sneakers', routeName: 'sneakers',
          items: [
            {id: 3,name: 'Adidas NMD',price: 220},
            {id: 4,name: 'Adidas Yeezy',price: 280}
          ]
        }
      ];
      
      //Question 1
      //creating the object that will hold the item
      //outside of the 'forEach' scope so we can use it later
      let res = {};
      const searchRes = DATA.forEach(entry => {
        entry.items.forEach(item => {
          if (item.name === 'Brown Brim')
            //duplicating the item to a variable declared outside of this scope
            res = { ...item
            };
        });
      });
      console.log(`Q1 - The item we found:`);
      console.log(res);
      
      // Question 2
      // Merging all object inside the 'items' array using Reduce
      const merged = DATA.reduce((acc, curr) =>
        acc.concat(curr.items), []);
      console.log(`Q2 - The merged array:`);
      console.log(merged)

      【讨论】:

      • @khdeveloperj,嗨,不客气。在这种情况下,请批准该答案作为您问题的答案
      猜你喜欢
      • 2019-06-13
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多