0

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"
        }         
]
0

1 Answer 1

2

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>

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

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.