将两个数组合并为一个具有键值关系的数组

时间:2016-04-09 23:17:46

标签: javascript

使用Javascript:  我有两个数组。一个有关键的另一个有值..我需要将它们在键值对中合并到一个数组中。

arr2 = ["anik","manik","philip"];
arr1 =[1,2,3];

how to make 
resultArray = ["anik:1","manik:2","philip:3"];

编辑1

试过

        Array.prototype.associate = function (keys) {
            var result = {};

            this.forEach(function (el, i) {
                result[keys[i]] = el;
            });

            return result;
        };
        var customerId = arr2;
        var customerName = arr3;
        console.log(customerId.associate(customerName));
        console.log(Object.keys(arr));
        //document.getElementById(customerId.associate(customerName));

解决

 array3 = [];
            console.log(arr2);
            arr2.forEach(function (e, i) {
                array3.push(
                  [e] + ":" + arr3[i]
                );
            })

            document.write(JSON.stringify(array3));

1 个答案:

答案 0 :(得分:1)

a b c ab ac abc acb ba bc bac bca ca cb cab cba

fiddle