1

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

0

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

what if groups is empty or undefined null ... ?
and is it possible to check if its empty string doesnt print it just if its an array ?
You can always filter out the results once you've mapped them @walee. If you want a quick and easy "filter all falsy" values out you could use mydata.flatMap((grup) => grup.groups).filter(Boolean). More advanced logic could be used within the filter if that doesn't suit your needs.
i used filter((e: any) => e) after it and it fixed it
0

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

Comments

0

You can use .flatmap() and need to check group is an array or not. 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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.