I have an array in the form:
[
{id:'4704',sub_elements:['123456','7890123','5566778']},
{id:'4706',sub_elements:['223344','7890123','1111111']},
....
]
The array is about 2500 elements with each sub array also having potentially hundreds of elements.
I wish to transform this array with the elements swapped. i.e.
[
'123456': ['4704'],
'7890123': ['4704','4706'],
'5566778': ['4704'],
'223344' : ['4706'],
'1111111' : ['4706']
]
One way is to take all the sub elements from the first array and push them onto an array. Then just de-dupe the array.
So you end up with something like:
var sub_elements = ['123456','7890123','5566778', '223344','7890123','1111111', ....] //50,000+ elements
Then just iterate over that (pretty massive list). Pseudocode:
var original = [
{id:'4704',sub_elements:['123456','7890123','5566778']},
{id:'4706',sub_elements:['223344','7890123','1111111']},
....
]; //2,000+ elements
var elements = ['123456','7890123','5566778', '223344','7890123','1111111'];
var result = {};
for(element in elements){
var ids = [];
for(var x in original){
if(original[x].sub_elements.indexOf(elements[element]) >= 0){
ids.push(original[x].id);
}
}
result[elements[element]] = ids;
}
Problem is with so many elements in the de-duped array this takes an absolute age in Node. There has to be a more efficient way of doing this. In reality the size of elements is 50K+ elements. So it's iterating over all 50K elements and for each iteration it iterates over the original array.
Wood for the trees at the moment - maybe someone here has done something like this already.