【问题标题】:Is there a way to filter an item in a array inside another array有没有办法过滤另一个数组中的数组中的项目
【发布时间】:2021-09-11 01:27:15
【问题描述】:

基本上我有一个对象,其中有一个数组,并且我有这个对象的一个​​高级数组。

对象如下。

category: {
   name,
   items: [{
      id,
      name,
      price,
      image,
   }],
}

这个数组看起来像:

[{
   name: "Category 1",
   items: [{
      id: 1,
      name: "Item 1 of category 1",
      price: 12.34,
      image: "/path/to/image",
   },
   {
      id: 2,
      name: "Item 2 of category 1",
      price: 56.78,
      image: "/path/to/image2",
   }]
},
{
   name: "Category 2",
   items: [{
      id: 3,
      name: "Item 1 of category 2",
      price: 87.65,
      image: "/path/to/image3",
   },
   {
      id: 4,
      name: "Item 2 of category 1",
      price: 43.21,
      image: "/path/to/image4",
   }]
}]

我的问题是,可以搜索id,因为我有一个包含所有这些数据的数组。

目前我正在使用以下代码解决问题:

var price = 0;
const menu = [];
state.user.menu.map((item, k) => {
  item.category.items.forEach((itm) => menu.push(itm));

  return null;
});

state.user.items.forEach((item) => {
  price += menu.filter((itm) => itm.id === item.id)[0].price * item.quantity;
});

这基本上复制了对象内部数组中的每个项目并忽略了类别名称,所以我只有一个大数组。

对于我现在需要的,我需要将每个项目与类别名称相关联,所以,我不能像现在这样。 基本上,我有一个Ids 列表,我需要将它们与该数组中的相应类别一起显示。

(Items to search)
[{
   timestamp: "123456",
   userid: "123456",
   ...
   id: 1,
   price: 12.34,
},
{
   timestamp: "123456",
   userid: "123456",
   ...
   id: 3,
   price: 87.65,
},
{
   timestamp: "123456",
   userid: "123456",
   ...
   id: 4,
   price: 43.21,
}]

(Expected Result)
[{
   name: "Category 1",
   items: [{
      id: 1,
      name: "Item 1 of category 1",
      price: 12.34,
      image: "/path/to/image",
   }]
},
{
   name: "Category 2",
   items: [{
      id: 3,
      name: "Item 1 of category 2",
      price: 87.65,
      image: "/path/to/image3",
   },
   {
      id: 4,
      name: "Item 2 of category 1",
      price: 43.21,
      image: "/path/to/image4",
   }]
}]

欢迎任何建议,谢谢。

【问题讨论】:

  • 请添加一些数据和想要的结果。
  • 我已经更新了问题
  • .map 用于返回转换后的数据,但您将其用于state.user.menu 的副作用。这可能会让其他开发人员感到困惑。 Array.protoype.map -> map() 方法创建一个新数组,其中填充了对调用数组中的每个元素调用提供的函数的结果。

标签: javascript arrays reactjs javascript-objects


【解决方案1】:

const data = [
  { name: "Category 1", items: [{ id: 1, name: "Item 1 of category 1", price: 12.34, image: "/path/to/image" }, { id: 2, name: "Item 2 of category 1", price: 56.78, image: "/path/to/image2" }] },
  { name: "Category 2", items: [{ id: 3,  name: "Item 1 of category 2",  price: 87.65, image: "/path/to/image3" }, { id: 4, name: "Item 2 of category 1", price: 43.21, image: "/path/to/image4" }] }
];

const getItemsById = (arr = []) => {
  // get list of ids to search for
  const ids = arr.map(({ id }) => id);
  // iterate over list of objects
  return data.reduce((list, elem) => {
    // get current items with ids
    const items = elem.items.filter(({ id }) => ids.includes(id));
    // if any found, add element with filtered list
    if(items.length > 0) list.push({ ...elem, items });
    return list;
  }, []);
}

console.log( getItemsById([{ id: 1 }, { id: 3 }, { id: 4 }]) );

【讨论】:

  • 好吧,这看起来很有希望,但我有点撒谎,我用来过滤的数组不仅仅是 id,它是另一个带有 id 和一些乱码数据的对象。所以在你有ids.includes(id)的地方我应该用state.user.itemsstate.user.items.id替换ids?因为当我不使用id 时,它只会抛出一个错误,而当我不使用时,我会得到正确的类别对象,但其中包含每个项目。
  • @GabrielValente 我不明白,你为什么不能提供真实的示例输入?
  • 是的,我的错,我认为这会更加混乱,因为在我的脑海中我只需要做.id 就可以得到它。无论如何,我已经更新了问题。
  • 您可以通过从更新的列表中生成一个来获取数组参数,如下所示:arr.map(({ id }) => id)。这样能解决问题吗?
  • 天哪,它正在工作,返回数据有点乱,但比我能做的要好得多。谢谢你的帮助,我很抱歉我把一切都复杂化了。
【解决方案2】:

这就是你要找的吗?

const obj = {
  category: {
     name: "Test",
     items: [{
        id: 1,
        name: "Test",
        price: 50,
        image: "Test",
     }],
  }
}

console.log(obj.category.items[0].id);

【讨论】:

  • 还有一点,因为我必须遍历第一个数组中的每个项目,在里面的数组中搜索一个 id。
猜你喜欢
  • 2021-07-29
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 2022-10-13
相关资源
最近更新 更多