首页 > 试题广场 >

对于一个数字组成的数组nums,现在需要执行在不改动nums

[单选题]
对于一个数字组成的数组 nums,现在需要执行在不改动 nums 的基础上去重操作,返回一个新的无重复元素的数组,以下几段代码能完成这一操作的是()
(1)
const newNums = Array.from(new Set(nums))
(2)
const newNums = nums.filter((n, i) => {
    return nums.indexOf(n) === i
})
(3)
const newNums = nums.forEach((n, i) => {
    return nums.indexOf(n) === i
})
(4)
const newNums = nums.reduce((acc, n, i) => {
    return [].concat(acc, nums.indexOf(n) === i ? n : []
)
})

  • (1)、(2)、(3)、(4)
  • (1)、(3)、(4)
  • (1)、(2)、(4)
  • (1)、(4)
filter返回一个新的、由通过测试的元素组成的数组,如果没有任何数组元素通过测试,则返回空数组。
forEach对每个元素进行操作,对原数组造成影响,无返回值。
发表于 2021-06-23 17:16:15 回复(0)