B
B
beefront172018-03-22 09:19:40
JavaScript
beefront17, 2018-03-22 09:19:40

How to trim each object in an array?

Hello! Can you please tell me the best way to cut each object in an array?
There is such an object with arrays

data = {
test1: [{id: '1', ...}, {id: '2', ...}, ...],
test2: [{id: '3', ...}, {id: '4', ...}, ...],
  test3: [],
  test4: [],
}

and you have to do this
data2 = {
test1: ['1', '2'...],
test2: ['3', '4' ...],
 test3: [],
 test4: [],
}

What is the best way to do this?
Lodash can be used.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kornachev, 2018-03-22
@beefront17

var resultTest1 = data.test1.map((o) => o.id)

A
Alexander, 2018-03-22
@satellite

In your case, this code

const data = { ... };
const data2 = {};
Object.keys(data).forEach(key => {
  data2[key] = data[key].map(obj => obj.id);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question