0

I've array like this

const data=[ {name:aa, status:[key:0, value: true] },
         {name:ee, status:[key:1, value: true] },
         {name:ii, status:[key:1, value: true] },
       ]

I want to convert it into like this

const data=[aa, ee, ii]

P.S. thank you.


tried like this

data.map(({ status, ...data }) => Object.values(data).concat);

Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136

2 Answers2

3

Assuming you have an array of objects, you need to take name from the object probably by destructuring and map this value.

const
    data = [{ name: 'aa', status: { key: 0, value: true } }, { name: 'ee', status: { key: 1, value: true } }, { name: 'ii', status: { key: 1, value: true } }],
    result = data.map(({ name }) => name);

console.log(result);
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
1

You also can solve your question with reduce.

const data = [
  { name: 'aa', status: { key: 0, value: true } },
  { name: 'ee', status: { key: 1, value: true } },
  { name: 'ii', status: { key: 1, value: true } },
];

const result = data.reduce((acc, { status, ...name }) => {
  acc[name] = acc.push(Object.values(name));

  return acc;
}, []).flat();

Or simply with for loop.

const result = [];

for (let obj of data) {
  result.push(obj.name);
}
Tigran Abrahamyan
  • 746
  • 1
  • 3
  • 7