King Agustin
King Agustin

Reputation: 61

How to merge these complex JavaScript array objects?

Can someone help me to do these example?

var a = ["17","18"];
var b = ["1","1","1"];

I need an output below:

var c = [17:111,18:111]

Upvotes: 0

Views: 49

Answers (2)

user5113650
user5113650

Reputation:

var a = ["17","18"];
var b = ["1","1","1"];
var i=0;
var ConcateC="";
for(i=0;i< b.length;i++)
{
    ConcateC +=b[i];
   
}
var c=[];
for(i=0;i< a.length;i++)
{
    c[i]=a[i] + ":" + ConcateC;
   alert(c[i]);
}

Upvotes: 2

CodingIntrigue
CodingIntrigue

Reputation: 78555

You can join the values of b together, then use map to create a new array from the indices of a:

var a = ["17","18"];
var b = ["1","1","1"];
var bValue = b.join("");
var c = a.map(function(currentValue) {
    return currentValue + ":" + bValue;
});
console.log(c); // ["17:111","18:111"]

Upvotes: 1

Related Questions