gpt4 book ai didi

javascript - 在多维数组中搜索字符串重复项

转载 作者:行者123 更新时间:2023-12-02 06:13:15 24 4
gpt4 key购买 nike

我有带字符串的多维数组:

const arr = [['a', 'b'], ['c', 'd'], ['d', 'a']]

如何记录控制台所有嵌套数组中超过1次的所有值? (对于此示例功能,应该使用console.log'a'和'd')。

谢谢您的帮助

最佳答案

let findDupesInMDArray = (arr) => {
// flatten the array
const flat = arr.flat();
// filter for dupes
return flat.filter((item, index) => flat.indexOf(item) != index)
}

let array = [['a', 'b'], ['c', 'd'], ['d', 'a']]
const dupes = findDupesInMDArray(array)
console.log(dupes)

关于javascript - 在多维数组中搜索字符串重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54652408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com