I have 2 arrayes in node .
['3', '7' ]
[ 'Circulatory and Cardiovascular', 'Respiratory' ]
I want to produce result as below.
{{"id": "3","name":"Circulatory and Cardiovascular"},{"id": "7","name":"Respiratory"}}
I'm sorry that's an array in output but it's easier. You could do this:
var idArray = ['3', '7' ];
var nameArray = [ 'Circulatory and Cardiovascular', 'Respiratory' ];
var newArray = [];
for (var i = 0; i < idArray.length; i++) {
newArray.push({"id": idArray[i], "name": nameArray[i]});
}
Output:
[ { id: '3', name: 'Circulatory and Cardiovascular' },
{ id: '7', name: 'Respiratory' } ]
I'm not sure that's a great idea, but you can convert your new array into an object like this:
var newObject = newArray.reduce(function(o, v, i) {
o[i] = v;
return o;
}, {});
Output:
{ '0': { id: '3', name: 'Circulatory and Cardiovascular' },
'1': { id: '7', name: 'Respiratory' } }
Or another way:
Object.setPrototypeOf(newArray, Object.prototype); // Now it's an object
Output:
Object [
{ id: '3', name: 'Circulatory and Cardiovascular' },
{ id: '7', name: 'Respiratory' } ]
Hope this help !
I assume you want to create this data structure (array instead of hash):
In that case you can use lodash like this: