简体   繁体   English

从对象数组中获取对象

[英]getting objects from from array of objects

I have an array of objects and some of those objects has groups which is an array,我有一个对象数组,其中一些对象有一个数组组,

i want all those objects from all groups and put it into one array of object, any idea how this could be done?我想要所有组中的所有这些对象并将其放入一个 object 数组中,知道如何做到这一点吗?

what i want to have is this: GroupData= [ { id: "679d8044", name: "group 3" }, { id: "b9342eb8", name: "group 1" } ];我想要的是: GroupData= [ { id: "679d8044", name: "group 3" }, { id: "b9342eb8", name: "group 1" } ];

any idea?任何的想法? english is not my mother language so could be mistakes.英语不是我的母语,所以可能会出错。

you can try it here: https://codesandbox.io/s/react-list-array-of-objects-forked-rk34fj?file=/src/index.js你可以在这里试试: https://codesandbox.io/s/react-list-array-of-objects-forked-rk34fj?file=/src/index.js

my code:我的代码:

 import React from "react"; import { render } from "react-dom"; const mydata = [ { name: "living room", groups: "" }, { groups: [ { id: "679d8044", name: "group 3" } ], name: "Area 1" }, { groups: [ { id: "b9342eb8", name: "group 1" } ], name: "Area 2" } ]; const GroupData = mydata.find((grup) => grup.groups); console.log(GroupData); const App = () => <div></div>; render(<App />, document.getElementById("root"));

You can accomplish this using flatMap .您可以使用flatMap完成此操作。

 const mydata = [ { name: "living room", groups: "" }, { groups: [ { id: "679d8044", name: "group 3" } ], name: "Area 1" }, { groups: [ { id: "b9342eb8", name: "group 1" } ], name: "Area 2" } ]; const GroupData = mydata.flatMap((grup) => grup.groups); console.log(GroupData)

I did notice you are using an empty string for your groups when there are no values.我确实注意到,当没有值时,您正在为您的组使用空字符串。 It's best to keep consistency within your data.最好保持数据的一致性。 In your example, I would recommend changing from groups: "" to groups: [] to indicate an empty group.在您的示例中,我建议从groups: ""更改为groups: []以指示一个空组。

 const mydata = [ { name: "living room", groups: [] }, { groups: [ { id: "679d8044", name: "group 3" } ], name: "Area 1" }, { groups: [ { id: "b9342eb8", name: "group 1" } ], name: "Area 2" } ]; const GroupData = mydata.flatMap((grup) => grup.groups); console.log(GroupData)

You can try this:你可以试试这个:

const mydata = [
  {
    name: "living room",
    groups: ""
  },
  {
    groups: [
      {
        id: "679d8044",
        name: "group 3"
      }
    ],
    name: "Area 1"
  },
  {
    groups: [
      {
        id: "b9342eb8",
        name: "group 1"
      }
    ],
    name: "Area 2"
  }
];

let groupData = [];

mydata.map((d) => {
  if (Array.isArray(d.groups)) {
    d.groups.map((group) => {
      groupData.push(group)
    })
    console.log(groupData)
  }
})

You can use .flatmap() and need to check group is an array or not.您可以使用.flatmap()并且需要检查group是否为数组。 If the group is not an array we don't need to include it in the final array, so instead we return [] so flatmap makes it an empty element.如果该group不是一个数组,我们不需要将它包含在最终数组中,所以我们返回[]所以flatmap使它成为一个空元素。

 const mydata = [{ name: "living room", groups: "" }, { groups: [ { id: "679d8044", name: "group 3" } ], name: "Area 1" }, { groups: [ { id: "b9342eb8", name: "group 1" } ], name: "Area 2" }]; const isArr = (arr) => Array.isArray(arr); const res = mydata.flatMap(({groups}) => (isArr(groups) && groups) || []); console.log(res);

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

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