Home > database >  getting objects from from array of objects
getting objects from from array of objects

Time:12-02

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 ?

what i want to have is this : 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

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"));

CodePudding user response:

You can accomplish this using 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.

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)

CodePudding user response:

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)
  }
})

CodePudding user response:

To extract all the objects from the groups arrays and put them into a single array, you can use the flatMap() method on the mydata array. The flatMap() method will map each element in the array to a new array, and then flatten the resulting arrays into a single array.

Here is an example of how you could use the flatMap() method to extract the objects from the groups arrays and put them into a single array:

const GroupData = mydata.flatMap((item) => item.groups);

After this code is executed, the GroupData array will contain the following elements:

[
  { id: "679d8044", name: "group 3" },
  { id: "b9342eb8", name: "group 1" }
]

Note that the flatMap() method will only extract objects from the groups arrays that are present in the mydata array. If an object in the mydata array does not have a groups array, it will be ignored by the flatMap() method.

  • Related