【问题标题】:Can't removes duplicates from json file无法从 json 文件中删除重复项
【发布时间】:2021-08-28 11:57:09
【问题描述】:

在这里,我试图从我的 json 文件中删除重复项,因此我尝试循环数据并从中删除重复项。 所以为此我写了这段代码:`

var newData = [];
for (i = 0; i < playerData.length; i++) {
  for (j = 0; j < playerData[i].allTeams.length; j++) {
    for (k = 0; k < playerData[i].allTeams[j].listOfPlayer.length; k++) {
      for (s = k + 1; s < playerData[i].allTeams[j].listOfPlayer.length; s++) {
        if (
          playerData[i].allTeams[j].listOfPlayer[k].name ==
            playerData[i].allTeams[j].listOfPlayer[s].name &&
          playerData[i].allTeams[j].listOfPlayer[k].playerURL ==
            playerData[i].allTeams[j].listOfPlayer[s].playerURL
        ) {
          delete playerData[i].allTeams[j].listOfPlayer[s].name;
          delete playerData[i].allTeams[j].listOfPlayer[s].playerRole;
          delete playerData[i].allTeams[j].listOfPlayer[s].playerURL;
        }
      }
    }
  }
  newData.push(playerData[i]);
}`

首先,我尝试在我的示例数据中使用此代码并且它成功运行,但是当我为文件 main.json 中的大量数据运行此代码时,它无法正常工作。 是什么阻止我这样做??

这里是代码和数据的链接: https://codesandbox.io/s/flamboyant-dewdney-40jw5?file=/src/index.js

【问题讨论】:

  • 在上面的链接中没有找到json的数据。请分享一个样本json 数据。
  • Yasin,您必须为适合您的解决方案投票。
  • Yasin,很好,但现在你有代码帮助继续前进。
  • Yasin,这里有几个答案可供选择。他们有没有解决你的问题?如果是这样,请标记一个作为答案。如果没有,请进一步澄清。谢谢

标签: javascript json fs


【解决方案1】:

尝试使用map() 进行迭代并使用.filter() 过滤掉重复项。这是使用您的 sample.json 数据的可测试 sn-p

const playerData = [{
  "clubName": "ADSK Cricket Club",
  "clubUrl": "https://cricclubs.com/baca/viewInternalClub.do?internalClubId=32&clubId=1755",
  "allTeams": [{
      "teamName": "Afgan Zalmi",
      "teamURL": "https://cricclubs.com/baca/viewTeam.do?teamId=393&clubId=1755",
      "listOfPlayer": []
    },
    {
      "teamName": "Autodesk CC",
      "teamURL": "https://cricclubs.com/baca/viewTeam.do?teamId=376&clubId=1755",
      "listOfPlayer": [{
          "name": "AMARNATH AMARNATH",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=893120&clubId=1755"
        },
        {
          "name": "AMARNATH AMARNATH",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=893120&clubId=1755"
        },
        {
          "name": "AMITH RAUL",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=885821&clubId=1755"
        },
        {
          "name": "ASHISH ASHISH",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=893119&clubId=1755"
        },
        {
          "name": "HARPREET KHAIRA",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=885820&clubId=1755"
        },
        {
          "name": "HEMANT MURARKA",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=885822&clubId=1755"
        },
        {
          "name": "HERSHAL K",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=943445&clubId=1755"
        },
        {
          "name": "VISHAL PHIRANI",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=885851&clubId=1755"
        },
        {
          "name": "IMRAN SYED",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=839822&clubId=1755"
        },
        {
          "name": "VISHAL PHIRANI",
          "playerRole": " All Rounder ",
          "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=885851&clubId=1755"
        }
      ]
    }
  ]
}]
const nplayerData = playerData.map( set => {
set.allTeams = set.allTeams.map( p => {
p.listOfPlayer = p.listOfPlayer.filter((t, i, s) =>
    i === s.findIndex((pl) => (
      pl.name === t.name && pl.playerURL.trim() === t.playerURL.trim()
    ))
  );
return p;
})
return set
})
console.log(nplayerData);

【讨论】:

    【解决方案2】:

    试试这两个功能:

    getUniqueListBy(arr, key):对象的唯一值,可以指定key

    getRemoveNestedDuplicates():遍历嵌套值

    const playerData = [
      {
        "clubName": "ADSK Cricket Club",
        "clubUrl": "https://cricclubs.com/baca/viewInternalClub.do?internalClubId=32&clubId=1755",
        "allTeams": [
          {
            "teamName": "Afgan Zalmi",
            "teamURL": "https://cricclubs.com/baca/viewTeam.do?teamId=393&clubId=1755",
            "listOfPlayer": []
          },
          {
            "teamName": "Autodesk CC",
            "teamURL": "https://cricclubs.com/baca/viewTeam.do?teamId=376&clubId=1755",
            "listOfPlayer": [
              {
                "name": "AMARNATH AMARNATH",
                "playerRole": " All Rounder ",
                "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=893120&clubId=1755"
              },
              {
                "name": "AMARNATH AMARNATH",
                "playerRole": " All Rounder ",
                "playerURL": "https://cricclubs.com/baca/viewPlayer.do?playerId=893120&clubId=1755"
              }
            ]
          }
        ]
      }
    ]
    
    
    function getUniqueListBy(arr, key) {
      if (Array.isArray(arr)) {
        return [...new Map(arr.map(item => [item[key], item])).values()]
      }
      return []
    }
    
    function getRemoveNestedDuplicates(data) {
      for (prop in data) {
        if (typeof(data[prop]) == 'object') {
          if (data[prop] && data[prop].listOfPlayer) {
            data[prop].listOfPlayer = getUniqueListBy(data[prop].listOfPlayer, 'name')
          } 
          getRemoveNestedDuplicates(data[prop])
        }
      }
    }
    
    getRemoveNestedDuplicates(playerData)
    
    
    for (const data of playerData) {
      console.log(data.allTeams[1].listOfPlayer)
    }

    【讨论】:

      【解决方案3】:

      请替换js 变量players 中的json 数据。这应该适合你。

      var newData = [];
      
      function removeDuplicates() {
      
        // Create an array of objects
        players = [{
            name: "James",
            playerrole: "Bjarne",
            playerURL:"Captain"
          },
          {
           name: "James",
            playerrole: "Bjarne",
            playerURL:"Captain"
          },
          {
            name: "James",
            playerrole: "Bjarne",
            playerURL:"Captain"
          },
          {
            name: "James",
            playerrole: "Bjarne",
            playerURL:"Captain"
          }
        ];
      
        jsonObject = players.map(JSON.stringify);
        uniqueSet = new Set(jsonObject);
        newData = Array.from(uniqueSet).map(JSON.parse);
      
        console.log(newData);
      
      }
      <p>
        Click on the button to remove the duplicates in the array
      </p>
      
      <p>Check the console for the output</p>
      
      <button onclick="removeDuplicates()">
        Click here
      </button>
       

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-04
        • 1970-01-01
        • 1970-01-01
        • 2015-03-23
        • 1970-01-01
        • 1970-01-01
        • 2018-12-04
        • 1970-01-01
        相关资源
        最近更新 更多