I have the array of objects as below which has a base object with a set of values.
I need to remove the base object of all the data and make it as the Expected result below.
Example array
[
{
"100": {
"id": "100",
"name": "Test name 1"
},
"101": {
"id": "101",
"name": "Test name 2"
},
"102": {
"id": "102",
"name": "Test name 3"
}
}
]
Expected Result
[
{
"id": "100",
"name": "Test name 1"
},
{
"id": "101",
"name": "Test name 2"
},
{
"id": "102",
"name": "Test name 3"
}
]
You can iterate with Array.map()
, get the values of the object with Object.values()
, and flatten the results to a single array by spreading into Array.concat()
:
const data = [{"100":{"id":"100","name":"Test name 1"},"101":{"id":"101","name":"Test name 2"},"102":{"id":"102","name":"Test name 3"}}];
const result = [].concat(...
data.map(o => Object.values(o))
);
console.log(result);
With lodash you can use _.flatMap()
with _.values()
:
const data = [{"100":{"id":"100","name":"Test name 1"},"101":{"id":"101","name":"Test name 2"},"102":{"id":"102","name":"Test name 3"}}];
const result = _.flatMap(data, _.values);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>