“Kurangi dua array JavaScript” Kode Jawaban

Diff Dua Array JavaScript

function diffArray(arr1, arr2) {
  return arr1
    .concat(arr2)
    .filter(item => !arr1.includes(item) || !arr2.includes(item));
}
Yawning Yacare

JavaScript mendapatkan perbedaan array

function arrayDiff (a1, a2) {
    var a = [], diff = [];
    for (var i = 0; i < a1.length; i++) {
        a[a1[i]] = true;
    }
    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            delete a[a2[i]];
        } else {
            a[a2[i]] = true;
        }
    }
    for (var k in a) {
        diff.push(k);
    }
    return diff;
}
//usage:
console.log(arrayDiff(['red', 'white','green'], [ 'red','white', 'blue']));//["green", "blue"]
Grepper

Kurangi dua array JavaScript

var array1 = [1,2,3,4,5,6.25];
var array2 = [9,8,7,6,5,4];

const diffNumber = (arr1, arr2) => arr1.map(function (num, idx) { return num- arr2[idx] });

diffNumber(array2, array1); //[8, 6, 4, 2, 0, -2.25]
nashrkra

Jawaban yang mirip dengan “Kurangi dua array JavaScript”

Pertanyaan yang mirip dengan “Kurangi dua array JavaScript”

Lebih banyak jawaban terkait untuk “Kurangi dua array JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya