删除3个数组

时间:2018-02-10 20:54:37

标签: javascript arrays ecmascript-6

我最近接受过技术面试。我被要求解决的代码挑战给出了3个数组。目标是删除所有重复和负面的nums 我能够删除重复但不重复,到目前为止,我有以下代码。我错过了什么?

let array1=[10, 200, 10, 200, 100, 100, 200, 200, 200, 200, -99, -6, 0, -859]
let array2 = [100, 200, 100, 200, 689, 689, 200, 400, 210, 200, -58, 200, -305, -6, 0, -859]
let array3 =[100, 200, 100, 200, 689, 689, 200, 400, 210, 400, -6, 200, -305, -6, 0, -859]

const arrays = {
    array1,array2,array3
}


let nodups=Array.from(new Set(array1.concat(array2,array3)))
console.log(nodups);

2 个答案:

答案 0 :(得分:1)

获取重复的可用数组后,您需要仅为正值过滤数组。



var array1 = [10, 200, 10, 200, 100, 100, 200, 200, 200, 200, -99, -6, 0, -859],
    array2 = [100, 200, 100, 200, 689, 689, 200, 400, 210, 200, -58, 200, -305, -6, 0, -859],
    array3 = [100, 200, 100, 200, 689, 689, 200, 400, 210, 400, -6, 200, -305, -6, 0, -859],
    result = Array
        .from(new Set(array1.concat(array2, array3)))
        .filter(v =>  v >= 0);

console.log(result);




答案 1 :(得分:0)

将它们放入Set中并应用过滤器

let array1=[10, 200, 10, 200, 100, 100, 200, 200, 200, 200, -99, -6, 0, -859]
let array2 = [100, 200, 100, 200, 689, 689, 200, 400, 210, 200, -58, 200, -305, -6, 0, -859]
let array3 =[100, 200, 100, 200, 689, 689, 200, 400, 210, 400, -6, 200, -305, -6, 0, -859]

let res = [...new Set([...array1, ...array2, ...array3])].filter(e => e >= 0);
console.log(res);