0

In my node app i have to construct an object array from another object array.

Consider my object array as..

[ { id_0: 356, id_1: 33, name_1: 'aaaa' },
  { id_0: 756, id_1: 89, name_1: 'bbbbb' },
  { id_0: 456, id_1: 89, name_1: 'ccccc' },
  { id_0: 356, id_1: 27, name_1: 'dddd' } ]

I have to construct an object array as like below:

[{
"356":["33":"aaaa","27":"ddddd"],------------->Changes made
"456":[{"89":"cccc"}],
"756":[{"89":"bbbbbbbb"}]
}]

I tried using async.map.But i cant get the right way to do it.Please help me to solve this.Thanks in advance...

2
  • What's the point of using async here? Commented Apr 9, 2014 at 6:34
  • It is Array.map() not async.map() and you don't usually call it that way, but as a method on an instance of an array. Commented Apr 9, 2014 at 6:35

1 Answer 1

5

You can use Array.prototype.reduce function, like this

console.log(data.reduce(function(result, current) {
    var obj = {};
    result[current.id_0] = result[current.id_0] || [];
    obj[current.id_1] = current.name_1;
    result[current.id_0].push(obj);
    return result
}, {}));

Output

{ '356': [ { '33': 'aaaa' }, { '27': 'dddd' } ],
  '456': [ { '89': 'ccccc' } ],
  '756': [ { '89': 'bbbbb' } ] }

If you want to convert this to an array of object, just wrap the result of data.reduce with [] like this

console.log([data.reduce(function(result, current) {
    ...
    ...
}, {})]);

Edit:

result[current.id_0] = result[current.id_0] || [];

this line makes sure that result[current.id_0] is an array. If the value of result[current.id_0] is truthy, then that value is rturned but if it is not, then [] will be returned. So, a new array will be created and assigned to result[current.id_0]. It is actually a shorthand for

if (result.hasOwnProperty(current.id_0) === false) {
    result[current.id_0] = [];
}

Edit 2: If you like to keep the grouped elements as an object, you could do like this

console.log(data.reduce(function(result, current) {
    result[current.id_0] = result[current.id_0] || {};
    result[current.id_0][current.id_1] = current.name_1;
    return result
}, {}));

Output

{ '356': { '27': 'dddd', '33': 'aaaa' },
  '456': { '89': 'ccccc' },
  '756': { '89': 'bbbbb' } }
Sign up to request clarification or add additional context in comments.

7 Comments

@Subburaj Please check the edit and let me know if you need more clarifications :)
@Subburaj Did you mean {"33":"aaaa","27":"ddddd"}? Because that would be very appropriate here?
@ thefourtheye one more thing.. Don't thing that i am outsourcing everything to you..I am new to this stuff thats y i have idea where to start.. See my edited post..
@Subburaj I just checked, but I don't see any updates.
@Subburaj I am sorry, now the question gets too specific and the original question is entirely different from this one. Can you please post that as a new question?
|

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.