从多维数组中删除重复项。数字和数字字符串相等

时间:2016-09-15 21:54:25

标签: types duplicates comparison-operators

我几乎失望,但无法弄清楚如何比较数字和数字字符串。

flattenAndUnique([["hello", "abc"], [true], ["abc"], [123, "123"]])
// should return
// ["hello", "abc", true, 123]

我把它弄平了,我有数字编号/字符串到字符串比较下来。

var flat = arr.reduce(function(arrA, arrB) {
    return arrA.concat(arrB);
});
var unique = flat.filter(function(el, pos) {
    return flat.indexOf(el) === pos;
});

1 个答案:

答案 0 :(得分:0)



let input = [["hello", "abc"], [true], ["abc"], [123, "123"]]

// you're trying to do a horrible thing
// so you get a horrible function like this
const unsafeParseInt = x => {
  let int = Number.parseInt(x, 10);
  return Number.isNaN(int) ? x : int;
}

const concat = (x,y) => x.concat(y);
const flatten = xs => xs.reduce(concat, []);
const uniques = xs => xs.filter((x,pos) => xs.indexOf(x) === pos);

let output = uniques(flatten(input).map(unsafeParseInt));

console.log(output);