Skip to content

元素重复数组中搜索 ​

本节目标:理解“元素重复数组中搜索”的核心思路,并能把它用于实际开发或面试表达。

const uniqueByKey = (list: any[]) => {
  const seen = new Set<string>()
  return list.filter((item) => {
    const k = String(item.key ?? item.value)
    if (seen.has(k)) return false
    seen.add(k)
    return true
  })
}