更新时间:2022-06-20 02:57:11
你可以使用 Array#filter
, Array #indexOf
和 Array#lastIndexOf
并仅返回共享相同索引的值。
You could use Array#filter
with Array#indexOf
and Array#lastIndexOf
and return only the values which share the same index.
var array = [1, 2, 3, 4, 4, 5, 5],
result = array.filter(function (a, _, aa) {
return aa.indexOf(a) === aa.lastIndexOf(a);
});
console.log(result);