I have an original data array that consists of the following structure (Note: the links are dummy links just for the purpose of this example)...
var densitySet = [
{ Name: "Node 1", Total: 1000, Link: "http://www.if4it.com" },
{ Name: "Node 2", Total: 1500, Link: "http://www.if4it.com" },
{ Name: "Node 3", Total: 700, Link: "http://www.if4it.com" },
{ Name: "Node 4", Total: 300, Link: "http://www.if4it.com" },
{ Name: "Node 5", Total: 1000, Link: "http://www.if4it.com" },
{ Name: "Node 6", Total: 900, Link: "http://www.if4it.com" },
{ Name: "Node 7", Total: 1090, Link: "http://www.if4it.com" },
{ Name: "Node 8", Total: 35, Link: "http://www.if4it.com" },
{ Name: "Node 9", Total: 1000, Link: "http://www.if4it.com" },
{ Name: "Node 10", Total: 99, Link: "http://www.if4it.com" }
];
I'd like to split the above array into two separate arrays, where the first array (called "totalsArray") would consist only of all Names and Totals (i.e. the first and second columns of the original array) and the second array (called "linksArray") would consist of all Names and Links (i.e. the first and third columns).
In other words, when done, the two new arrays would contain the following...
var totalsArray = [
{ Name: "Node 1", Total: 1000 },
{ Name: "Node 2", Total: 1500 },
{ Name: "Node 3", Total: 700 },
{ Name: "Node 4", Total: 300 },
{ Name: "Node 5", Total: 1000 },
{ Name: "Node 6", Total: 900 },
{ Name: "Node 7", Total: 1090 },
{ Name: "Node 8", Total: 35 },
{ Name: "Node 9", Total: 1000 },
{ Name: "Node 10", Total: 99 }
];
var linksArray = [
{ Name: "Node 1", Link: "http://www.if4it.com" },
{ Name: "Node 2", Link: "http://www.if4it.com" },
{ Name: "Node 3", Link: "http://www.if4it.com" },
{ Name: "Node 4", Link: "http://www.if4it.com" },
{ Name: "Node 5", Link: "http://www.if4it.com" },
{ Name: "Node 6", Link: "http://www.if4it.com" },
{ Name: "Node 7", Link: "http://www.if4it.com" },
{ Name: "Node 8", Link: "http://www.if4it.com" },
{ Name: "Node 9", Link: "http://www.if4it.com" },
{ Name: "Node 10", Link: "http://www.if4it.com" }
];
In my real situation, the original array ("densitySet") can be VERY long so my question is: What is the fastest and most efficient way to declare the two new arrays and iterate through the original array to populate them?
My original code looks like:
var totalsArray = [];
var linksArray = [];
for(var i = 0; i < densitySet.length; i++){
var tempArray1 = {Name: densitySet[i].Name, Total: densitySet[i].Total};
var tempArray2 = {Name: densitySet[i].Name, Link: densitySet[i].Link};
totalsArray.push( tempArray1 );
linksArray.push( tempArray2 );
};
However, I don't know that this is the FASTEST and most EFFICIENT way to create the two new arrays...
Thanks, in advance, for any help you can offer.