0

I have an object like this:

data = {
    0: [{name: 'ABC', age: '43'}, {name: 'DEF', age: '20'}],
    1: [{name: 'GHI', age: '41'}, {name: 'JKL', age: '25'}],
    2: [{name: 'MNO', age: '19'}, {name: 'PQR', age: '24'}]
};

I want to merge the array values of the keys, and make a single array of objects like this:

[ {name: 'ABC', age: '43'}, {name: 'DEF', age: '20'}, {name: 'GHI', age: '41'}, {name: 'JKL', age: '25'}, {name: 'MNO', age: '19'}, {name: 'PQR', age: '24'} ]

I went through the Lodash docs to find something, but cannot come up with the right combination. Does anyone know how to do this in a concise way, with Lodash (preferably), or something else? Thanks in advance!!

5 Answers 5

2

Extract the arrays using _.values(), and apply concat to the arrays:

var data = {
    0: [{name: 'ABC', age: '43'}, {name: 'DEF', age: '20'}],
    1: [{name: 'GHI', age: '41'}, {name: 'JKL', age: '25'}],
    2: [{name: 'MNO', age: '19'}, {name: 'PQR', age: '24'}]
};

var result = Array.prototype.concat.apply([], _.values(data));
    
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>

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

Comments

1

You just need to convert the array like object to an array, then flatten it.

data = {
    0: [{name: 'ABC', age: '43'}, {name: 'DEF', age: '20'}],
    1: [{name: 'GHI', age: '41'}, {name: 'JKL', age: '25'}],
    2: [{name: 'MNO', age: '19'}, {name: 'PQR', age: '24'}]
};

console.log(
  _.flatten(_.toArray(data))
)
<script src="https://cdn.jsdelivr.net/lodash/4.16.4/lodash.min.js"></script>

Comments

1

Without lodash, just plain old JS:

var data = {
    0: [{name: 'ABC', age: '43'}, {name: 'DEF', age: '20'}],
    1: [{name: 'GHI', age: '41'}, {name: 'JKL', age: '25'}],
    2: [{name: 'MNO', age: '19'}, {name: 'PQR', age: '24'}]
};

var result = [];
for (var key in data) {
    if (data.hasOwnProperty(key)) {
        result = result.concat(data[key]);
    }
}
console.log(result);

Comments

1

With the new Object.values() you may do as follows in pure JS ES6;

var data = { 0: [{name: 'ABC', age: '43'}, {name: 'DEF', age: '20'}],
             1: [{name: 'GHI', age: '41'}, {name: 'JKL', age: '25'}],
             2: [{name: 'MNO', age: '19'}, {name: 'PQR', age: '24'}]
           },
 newData = [].concat(...Object.values(data));
console.log(newData);

Comments

1

Here is another pure javascript version.

var data = {
    0: [{name: 'ABC', age: '43'}, {name: 'DEF', age: '20'}],
    1: [{name: 'GHI', age: '41'}, {name: 'JKL', age: '25'}],
    2: [{name: 'MNO', age: '19'}, {name: 'PQR', age: '24'}]
};

var newdata = Object.keys(data).reduce(function (a,b) { 
  return a.concat(data[b]);
}, []);

console.log(newdata);

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.