比较两个数组,并用第三个数组的值替换重复的数组

时间:2021-11-30 12:21:09
var array1 = ['a','b','c','d'];
var array2 = ['a','v','n','d','i','f'];

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

Just starting to learn Javascript, I can't figure out how to compare the values of array2 to those on array1 and if so replace it with the corresponded array index from array3.

刚开始学习Javascript,我就不知道如何将array2的值与array1的值进行比较,如果是的话,用array3对应的数组索引替换它。

To turn it like this:

把它变成这样:

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

But also, it has to compare the values from array1 and array2 even if the index positions are different like this:

但是,它也必须比较array1和array2的值,即使索引位置像这样不同:

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

Thanks for the help

谢谢你的帮助

7 个解决方案

#1


0  

You could use a hash table and check against. If the string is not included in the hash table, a replacement value is set for this element.

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

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);

ES6 with Set

ES6与设置

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);

#2


1  

Use Array.prototype.reduce to check the duplicates and create a new array - see demo below:

使用Array.prototype。减少检查重复和创建一个新的数组-见下面的演示:

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);

#3


1  

You can use map() on array2 and see if current element is same as element in array1 with same index if it is return element from array3 with index of i else return current element or e

您可以在array2上使用map(),看看当前元素是否与array1中的元素具有相同的索引,如果它是从array3返回的元素,索引i else返回当前元素或e

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.map(function(e, i) {
  return e == array1[i] ? array3[i] : e;
})

console.log(result)

#4


1  

Use Array#map

使用数组#地图

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

#5


1  

there are many forms, this is a basic form:

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

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

#6


0  

Here you have some code that does what you ask:

这里有一些代码,可以按照您的要求:

    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];
            }
        }

Hope it helps!

希望它可以帮助!

#7


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

#1


0  

You could use a hash table and check against. If the string is not included in the hash table, a replacement value is set for this element.

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

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);

ES6 with Set

ES6与设置

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);

#2


1  

Use Array.prototype.reduce to check the duplicates and create a new array - see demo below:

使用Array.prototype。减少检查重复和创建一个新的数组-见下面的演示:

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);

#3


1  

You can use map() on array2 and see if current element is same as element in array1 with same index if it is return element from array3 with index of i else return current element or e

您可以在array2上使用map(),看看当前元素是否与array1中的元素具有相同的索引,如果它是从array3返回的元素,索引i else返回当前元素或e

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.map(function(e, i) {
  return e == array1[i] ? array3[i] : e;
})

console.log(result)

#4


1  

Use Array#map

使用数组#地图

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

#5


1  

there are many forms, this is a basic form:

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

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

#6


0  

Here you have some code that does what you ask:

这里有一些代码,可以按照您的要求:

    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];
            }
        }

Hope it helps!

希望它可以帮助!

#7


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