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

标签 javascript arrays sorting

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

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/

相关文章:

c++ - 如何调整结构元素数组的大小

javascript - 如何根据数组将单词拆分为所有可能的组合

php - WooCommerce 中标题由字母和数字组成时的自定义排序产品

javascript - 在 jquery 中隐藏一个 div

javascript - 获取 JSON 名称的值

javascript - 测试日期集合在哪些范围内不重叠

c - 是什么导致这些数组值不匹配?

javascript - 如何使 Youtube 数据搜索 API 开放供公众使用?

MySQL 按字母数字排序不起作用

ruby - 如何按字母顺序对数组进行排序?