I have few records, and I want to merge them into an array. Here is some sample data:
var data = [{
name:'book1',
category: "nurse",
location: "US edition"
},
{
name:'book2',
category: "nurse",
location: "US edition"
},
{
name:'book3',
category: "patient",
location: "CAN edition"
}]
My idea as to what the end-result should be:
var data = [{
[name:'book1',name:'book2'],
category: "nurse",
location: "US edition"
},
{
name:'book3',
category: "patient",
location: "CAN edition"
}]
I tried to combine the records, however I am unable to find, and merge, the names. Here is the code that I've written so far:
var data = [
{
name:'book1',
category: "nurse",
location: "US edition"
},
{
name:'book2',
category: "nurse",
location: "US edition"
},
{
name:'book3',
category: "patient",
location: "CAN edition"
}
],
i = 0,
j = data.length - 1,
current;
for (;i < data.length; i++) {
current = data[i];
for (;j > i; j--) {
if (current.asset === data[j].asset && current.location === data[j].location) {
if (Array.isArray(current.data)) {
current.data = current.data.concat([data[j].data]);
} else {
current.data = [].concat([data[j].data, current.data]);
}
data.splice(j, 1);
}
}
}
console.log(data);
Can someone suggest to me, a way that I could merge my data into an array?
category
, thenname
should be the array,name: ['book1','book2']