Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have n (but for now, let say just two) of one dimensional arrays like this image of my console :

enter image description here

And I want to merge these two arrays by the corresponding key and put it into two dimensional array :

The result is something like :

[["1 279 226,08" , "127"],[null , null],["-188 033,77", "154"],..... so on ......]

And the list of one dimensional array is dynamic, it could be more than 2 arrays.

So example if I have 3 arrays, then my two dimensional array would look like :

[ ["1 279 226,08" , "127" , "blabla"], [null , null , "blabla"], ["-188 033,77", "154", "blabla"], ..... so on ......]

Any ideas of implementing it would be appreciate.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
152 views
Welcome To Ask or Share your Answers For Others

1 Answer

Since all of your arrays have the same size, you can loop just on the lenght of the first array, i.e. a.

Then we pass to appendArrays the single value of a , b, ..., and we return the an array to push into merged

var a = ["123", null, "ciao"]
var b = ["321", 1, "pippo"]
var c = ["111", 5, "co"]

var merged = []

for (i = 0; i < a.length; i++) {
  merged.push(appendArrays(a[i], b[i], c[i]));
}
console.log(merged);

function appendArrays() {
  var temp = []
  for (var i = 0; i < arguments.length; i++) {
    temp.push(arguments[i]);
  }
  return temp;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...