【问题标题】:How to fetch the all objects from the array of array? [duplicate]如何从数组的数组中获取所有对象? [复制]
【发布时间】:2023-01-25 14:40:40
【问题描述】:

我需要从 array of array 中获取所有对象,但我无法找到,我有任何 array 方法可以从 array of array 中找出对象。

const array1 = [
[{
   id:1,
   name:"test1"
 },
{
   id:2,
   name:"test2"
},
{
   id:3,
   name:"test3"
},
],
[{
   id:4,
   name:"test4"
 },
{
   id:5,
   name:"test5"
},
{
   id:6,
   name:"test6"
},
],
[{
   id:7,
   name:"test7"
 },
{
   id:8,
   name:"test8"
},
{
   id:9,
   name:"test9"
},
],

]

我只需要我可以使用数组方法获取数组中的所有对象

【问题讨论】:

  • 你到底想要什么?将所有对象放在一个数组中?
  • 也许.flat
  • 是的,我需要单个数组中的所有对象

标签: javascript reactjs angular vue.js


【解决方案1】:

只需使用flat 方法。

flat() 方法创建一个包含所有子数组元素的新数组 递归地连接到指定的深度。

const array1 = [
  [{
      id: 1,
      name: "test1",
    },
    {
      id: 2,
      name: "test2",
    },
    {
      id: 3,
      name: "test3",
    },
  ],
  [{
      id: 4,
      name: "test4",
    },
    {
      id: 5,
      name: "test5",
    },
    {
      id: 6,
      name: "test6",
    },
  ],
  [{
      id: 7,
      name: "test7",
    },
    {
      id: 8,
      name: "test8",
    },
    {
      id: 9,
      name: "test9",
    },
  ],
];
let result = array1.flat()
console.log(result);

【讨论】:

    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多