Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash: combine array of objects

In lodash, I want to convert an array of objects to a single object that contains an array of each property.

I have an array of objects:

var students = [{
    name: 'A',
    idNo: 1,
    marks: {
        math: 98,
        sci: 97,
        eng: 89
    }
}, {
    name: 'B',
    idNo: 2,
    marks: {
        math: 88,
        sci: 87,
        eng: 79
    }
}, {
    name: 'C',
    idNo: 3,
    marks: {
        math: 87,
        sci: 98,
        eng: 91
    }
}]

I want to combine / reshape them like this:

{
    name: [A, B, C],
    idNo: [1, 2, 3],
    marks: [{
            math: 98,
            sci: 97,
            eng: 89
        }, {

            math: 88,
            sci: 87,
            eng: 79            
    }, {
            math: 87,
            sci: 98,
            eng: 91
        }
    }]
}

I want this to be done purely with lodash or js inbuilt functions without any loops.

Edit : I have already implemented a solution as suggested by Nenad. I want a utility function in lodash.

like image 341
jgr0 Avatar asked Oct 13 '16 10:10

jgr0


People also ask

How do I merge arrays in Lodash?

Lodash proves to be much useful when working with arrays, strings, objects etc. It makes math operations and function paradigm much easier, concise. The _. concat() function is used to concatenating the arrays in JavaScript.

How do you merge an array of objects into a single object?

assign() method to convert an array of objects to a single object. This merges each object into a single resultant object. The Object. assign() method also merges the properties of one or more objects into a single object.

What is merge in Lodash?

Lodash helps in working with arrays, strings, objects, numbers, etc. The _. merge() method is used to merge two or more objects starting with the left-most to the right-most to create a parent mapping object.


1 Answers

You could use lodash's mapValues function:

var result = _.mapValues(students[0], (value, key) => _.map(students, key));

mapValues creates an object with the same keys as the object passed to it as the first parameter. Here we pass the first object in the students array students[0] so the object returned by mapValues will look something like this:

{
    name: ....,
    idNo: ....,
    marks: ....
}

The value for each key is determined by the function that is passed to mapValues as the second parameter. mapValues will call this function for each key and pass the value and the key. We're not interested in the value but the key is used to pluck all the values in the student array for that key (by calling map with the key as the second parameter).

var students = [{
    name: 'A',
    idNo: 1,
    marks: {
        math: 98,
        sci: 97,
        eng: 89
    }
}, {
    name: 'B',
    idNo: 2,
    marks: {
        math: 88,
        sci: 87,
        eng: 79
    }
}, {
    name: 'C',
    idNo: 3,
    marks: {
        math: 87,
        sci: 98,
        eng: 91
    }
}]


var result = _.mapValues(students[0], (value, key) => _.map(students, key));

document.getElementById('result').textContent = JSON.stringify(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

<p>
  <pre id="result"></pre>
</p>
like image 124
Gruff Bunny Avatar answered Oct 17 '22 09:10

Gruff Bunny