如何合并这些复杂的JavaScript数组对象?

时间:2015-08-19 10:11:12

标签: javascript arrays javascript-objects

有人可以帮助我做这些例子吗?

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

我需要一个输出:

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

2 个答案:

答案 0 :(得分:2)



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]);
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以将b的值加在一起,然后使用mapa索引创建一个新数组:

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"]