【问题标题】:retrieve the ids of an array检索数组的 id
【发布时间】:2020-09-23 12:39:07
【问题描述】:

当我做一个:

console.log(this.productService.getSortedProduct(graphicData)['finalProductList'])

我明白了:

0: []
1: []
2: []
3: []
4: []
5: []
6: []
7: Array(2)
   0: {products_id: 1039992, date: "2020-08-20"}
   1: {products_id: 1039997, date: "2020-08-20"}
8: []
9: []
10: []
11: []
12: Array(2)
   0: {products_id: 1039995, date: "2020-08-20"}
   1: {products_id: 1039996, date: "2020-08-20"}

如何从表中检索 products_Id 的所有值?

我在 google 上看到,我需要成为一个方法吗?

谢谢

【问题讨论】:

    标签: angular


    【解决方案1】:

    您可以通过遍历数组来找到它。如果元素有值,则检索其元素,否则跳过它的迭代。

    请使用简单的for 循环找到以下工作逻辑:

    const arr = [
      [],
      [],
      [],
      [],
      [],
      [],
      [],
      [{
          products_id: 1039992,
          date: "2020-08-20"
        },
        {
          products_id: 1039997,
          date: "2020-08-20"
        },
      ],
      [],
      [],
      [],
      [],
      [{
          products_id: 1039995,
          date: "2020-08-20"
        },
        {
          products_id: 1039996,
          date: "2020-08-20"
        }
      ]
    ];
    
    let result = [];
    
    for (const item of arr) {
      if (item.length) {
        result = [...result, ...item.map(value => value.products_id)];
      }
    }
    console.log(result);

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 2011-05-22
    • 2020-05-15
    • 2018-06-22
    • 2021-01-17
    • 1970-01-01
    • 2022-01-18
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多