比较两个Arrays并将Duplicates替换为第三个数组中的值

时间:2016-12-28 16:54:32

标签: javascript arrays

var array1 = ['a','b','c','d'];
var array2 = ['a','v','n','d','i','f'];

var array3 = ['1','2','3','4','5','6'];

刚刚开始学习Javascript,我无法弄清楚如何将array2的值与array1上的值进行比较,如果是这样,则将其替换为来自{{1}的对应数组索引}}

将其改为:

array3

但是,它必须比较array1和array2中的值,即使索引位置不同,如下所示:

array2 = ['1','v','n','4','i','f'];

感谢您的帮助

7 个答案:

答案 0 :(得分:1)

使用Array.prototype.reduce检查重复项并创建一个新数组 - 请参阅下面的演示:

var array1 = ['a','b','c','d'];
var array2 = ['a','v','n','d','i','f'];

var array3 = ['1','2','3','4','5','6'];

var result = array2.reduce(function(p,c,i){
  if(array1.indexOf(c) !== -1) {
     p.push(array3[i]);
  } else {
     p.push(c);
  }
  return p;
},[]);

console.log(result);

答案 1 :(得分:1)

你可以在array2上使用map(),看看当前元素是否与array1中具有相同索引的元素相同,如果它是来自array3的返回元素,索引为i,则返回当前元素或{{1 }}

e

答案 2 :(得分:1)

使用Array#map

array2.map((v, i) => v === array1[i] ? array3[i] : v);

答案 3 :(得分:1)

有很多形式,这是一种基本形式:

var array1 = ['a','b','c','d'];
var array2 = ['a','v','n','d','i','f'];
var array3 = ['1','2','3','4','5','6'];
var result = [];//define array of result
for(var i=0;i<array2.length;i++){//Iterate the array2
    if(array2[i] == array1[i])//Compare if array1 in index 'i' with array2 in index 'i'
        result[i] = array3[i];//if true put in result in index 'i' from array3
    else
        result[i] = array2[i];//else put in result in index 'i' from array2
}
console.log(result);//show in console the result

答案 4 :(得分:0)

这里有一些代码可以满足您的要求:

    var array1 = ['a','b','c','d'];
    var array2 = ['a','v','n','d','i','f'];
    var array3 = ['1','2','3','4','5','6'];
    var biggerArrayLength = 0;

        if(array1.length > array2.length){
            biggerArrayLength = array1.length;
        }else{
            biggerArrayLength = array2.length;
        }

        for(var i = 0; i < biggerArrayLength; i++){
            if(array1[i] == array2[i]){
                array2[i] = array3[i];
            }
        }

希望它有所帮助!

答案 5 :(得分:0)

您可以使用哈希表并进行检查。如果字符串未包含在哈希表中,则为该元素设置替换值。

&#13;
&#13;
var array1 = ['a','b','c','d'],
    array2 = ['d','v','n','a','i','f'],
    array3 = ['1','2','3','4','5','6'],
    hash = Object.create(null);

array1.forEach(function (a) {
    hash[a] = true;
});

array2.forEach(function (a, i, aa) {
    if (hash[a]) {
        aa[i] = array3[i];
    }
});

console.log(array2);
&#13;
&#13;
&#13;

ES6与Set

&#13;
&#13;
var array1 = ['a','b','c','d'],
    array2 = ['d','v','n','a','i','f'],
    array3 = ['1','2','3','4','5','6'];

array2.forEach((hash => (a, i, aa) => {
    if (hash.has(a)) {
        aa[i] = array3[i];
    }
})(new Set(array1)));

console.log(array2);
&#13;
&#13;
&#13;

答案 6 :(得分:0)

function replaceDuplicates(array1, array2, array3) {

    // array3 can't be smaller than array1!
    if (array3.length < array1.length) throw new Error('array3 < array1');

    // Loop through all the items in array1...
    for (var i = 0; i < array1.length; i++) {

        // Check if the item in array2 matches...
        if (i < array2.length && array2[i] === array1[i]) {

            // And if it does replace array1's item with array3's item!
            array1[i] = array3[i];

        }
    }
}


var array1 = ['a','b','c','d'];
var array2 = ['a','v','n','d','i','f'];
var array3 = ['1','2','3','4','5','6'];

replaceDuplicates(array1, array2, array3);

console.log(array1); // ['1','v','n','4','i','f']