Compare two Arrays and replace Duplicates with values from a third array

前端 未结 7 1904
不思量自难忘°
不思量自难忘° 2021-01-22 11:54
var array1 = [\'a\',\'b\',\'c\',\'d\'];
var array2 = [\'a\',\'v\',\'n\',\'d\',\'i\',\'f\'];

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

Just s

相关标签:
7条回答
  • 2021-01-22 12:33
    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']
    
    0 讨论(0)
  • 2021-01-22 12:36

    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
    
    0 讨论(0)
  • 2021-01-22 12:44

    Use Array#map

    array2.map((v, i) => v === array1[i] ? array3[i] : v);
    
    0 讨论(0)
  • 2021-01-22 12:48

    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!

    0 讨论(0)
  • 2021-01-22 12:53

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

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

    0 讨论(0)
  • 2021-01-22 12:53

    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

    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)

    0 讨论(0)
提交回复
热议问题