Javascript merge to array with some identical value at certain index -
i have 2 arrays want combine (merge), if value @ index 0 first array identical value @ index 0 on second array must in same array. more details: array 1:
var arr1 = [ ["sp.22110000295", 1, 162.9], ["sp29372", 1, 1061.6], ["sp3204200014", 5, 1147.5], ["sp324035", 1, 94110.0231] ];
array 2:
var arr2 [ ["999974", 1], ["sp.sambacr803805", 1], ["sp29193", 1], ["sp29372", 4], ["sp324035", 3] ]
as can see (in example), first array have 4 array 3 items, , second 5 array 2 items. arr1[3][0] (["sp324035", 1, 94110.0231]) equal arr2[4][0] (["sp324035", 1])
so don't know hot achieve result this:
var arrfinal = [ ["sp.22110000295", 1, 162.9, null], // arr1 index 0 **is not** in arr2 index 0 ["sp29372", 1, 1061.6, 4], // arr1 index 0 **is** in arr2 index 0 ["sp3204200014", 5, 1147.5, null], // arr1 index 0 **is not** in arr2 index 0 ["sp324035", 1, 94110.0231, 3], // arr1 index 0 **is** in arr2 index 0 ["999974", null, null, 1], // arr2 index 0 **is not** in arr1 index 0 ["sp.sambacr803805",null, null, 1], // arr2 index 0 **is not** in arr1 index 0 ["sp29193", null, null, 1] // arr2 index 0 **is not** in arr1 index 0 ];
actualy it's not realy merge, because arrays values of index 0 equals merged others it's kind of splice.
i tried merge, expand splice... underscore, jquery...
any hints please?
thank you, much, geo
what understand question have 2 arrays, arr1
, arr2
, , want generate new array, arrfinal
, folowing conditions
- items
arr1
should copiedarrfinal
- if there exists
arrfinal[i][0] === arr2[j][0]
i, j, items ofarr[2][j]
index1
should appened end of itemsarr1[i]
inarrfinal
- if not happen, push new array
arrfinal
index0
ofarr2[j][0]
paddednull
s before appending other itemsarr2[j]
- all items in
arrfinal
should have same length
var arrfinal = [], key_map = {}, max_len_1 = 0, max_len_2 = 0, i, j; (i = 0; < arr1.length; ++i) { max_len_1 = math.max(max_len_1, arr1[i].length); // find maximum length of arr1 arrfinal.push(arr1[i].slice()); // copy final key_map[arr1[i][0]] = arrfinal.length - 1; // mapping later } (i = 0; < arrfinal.length; ++i) { // fix lengths while (arrfinal[i].length < max_len_1) { arrfinal[i].push(null); } } (i = 0; < arr2.length; ++i) { max_len_2 = math.max(max_len_2, arr2[i].length); // find maximum length of arr2 if (key_map.hasownproperty(arr2[i][0])) { // entry exists arr1 j = key_map[arr2[i][0]]; } else { // generate new entry j = arrfinal.push([arr2[i][0]]) - 1; while (arrfinal[j].length < max_len_1) { // fix length arrfinal[j].push(null); } } array.prototype.push.apply( arrfinal[j], arr2[i].slice(1) ); } (i = 0; < arrfinal.length; ++i) { // fix lengths 1 last time while (arrfinal[i].length < max_len_1 + max_len_2 - 1) { arrfinal[i].push(null); } }
it should noted however, may give unexpected results if there exists multiple items have same value @ index 0
per arr1
or arr2
, example
var arr1 = [ [ 'foo', 1], ['fizz', 'buzz'] ]; var arr2 = [ ['foo', 'bar'] ['foo', 'baz'] ];
would result in
[ [ 'foo', 1, 'bar', 'baz'], ['fizz', 'buzz', null ] ]
further, if these guaranteed unique perhaps should consider using object instead of array
Comments
Post a Comment