javascript - 检查动态填充的数组中的重复对象

标签 javascript arrays loops for-loop

我正在尝试构建一个数据结构,其中所有元素都将根据对象键进行分组。

一切正常,除了我无法检查新数组是否有重复的数据,因为它在 for..of 循环之外。我正在寻找一种方法来防止 pushing 另一个对象,如果新数组已经有了它的话。

当前输出(注意来自日本的字符列表出现了两次)

[
  [
    { "country": "US" },
    [
      { "name": "Guile", "country": "US" }
    ]
  ],
  [
    { "country": "Japan" },
    [
      { "name": "E. Honda", "country": "Japan" },
      { "name": "Ryu", "country": "Japan" }
    ]
  ],
  [
    { "country": "Japan" },
    [
      { "name": "E. Honda", "country": "Japan" },
      { "name": "Ryu", "country": "Japan" }
    ]
  ],
  [
    { "country": "Thailand" },
    [
      { "name": "Sagat", "country": "Thailand" }
    ]
  ]
]

预期输出

[
  [
    { "country": "US" },
    [
      { "name": "Guile", "country": "US" }
    ]
  ],
  [
    { "country": "Japan" },
    [
      { "name": "E. Honda", "country": "Japan" },
      { "name": "Ryu", "country": "Japan" }
    ]
  ],
  [
    { "country": "Thailand" },
    [
      { "name": "Sagat", "country": "Thailand" }
    ]
  ]
]

到目前为止我有什么

var data = [
  {name: 'Guile', country: 'US'},
  {name: 'E. Honda', country: 'Japan'},
  {name: 'Ryu', country: 'Japan'},
  {name: 'Sagat', country: 'Thailand'}
]

const getNationList = (streetFighterList) => {
  let filteredList = []
  for (const [index, characterData] of streetFighterList.entries()) {
    // .......................................................
    // looking for ways here to check if `filteredList` already
    // has the data I'm trying to push. Since it's empty
    // I don't know how to check its index. :(
    // NOTE: indexOf() doesn't seem to work
    // .......................................................
    const indexOf = filteredList.indexOf(streetFighterList[index].country)
    if (indexOf == -1) {
      filteredList.push([
        { country: characterData.country },
        streetFighterList.filter((character) => {
          return character.country === characterData.country
        })
      ])
    }
  }
  return filteredList
}

console.log(getNationList(data))

注意:我知道鉴于 country 对象始终是唯一的,如果我改用字符串,这种数据结构会更好更容易。然而,这是一个示例数据,在现实生活中的代码中,我确实需要将它存储为一个对象。

最佳答案

我建议使用一些来验证如下

var data = [
  {name: 'Guile', country: 'US'},
  {name: 'E. Honda', country: 'Japan'},
  {name: 'Ryu', country: 'Japan'},
  {name: 'Sagat', country: 'Thailand'}
]

const getNationList = (streetFighterList) => {
  let filteredList = []
  for (const [index, characterData] of streetFighterList.entries()) {

    const entry = filteredList.some(item => item[0].country === streetFighterList[index].country)
    if (!entry) {
      filteredList.push([
        { country: characterData.country },
        streetFighterList.filter((character) => {
          return character.country === characterData.country
        })
      ])
    }
  }
  return filteredList
}

console.log(getNationList(data))

关于javascript - 检查动态填充的数组中的重复对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55421969/

相关文章:

javascript - prop 返回 null

c - void** 指针和 void*[] 作为函数参数

javascript - 如何在 JavaScript 中转换文档数组中的字典?

Windows 批处理文件 - 去除前导字符

javascript - 无法提交表单 - ReactJs

javascript - 即使在 wp_register_script 依赖项数组中定义,jQuery 也未在 WordPress 主题中定义

javascript - 从 javascript 模块属性获取未定义

python - 读取由空行分隔的txt数据作为多个numpy数组

loops - Prolog - 制作一个不执行任何操作并永远运行的应用程序

java - 输入空行时退出java程序