【问题标题】:Cannot find duplicates properly while comparing two arrays比较两个数组时无法正确找到重复项
【发布时间】:2020-01-29 21:35:28
【问题描述】:

我有两个Array 的问题。我必须比较才能仅在必要时创建,否则更新。

这是我的代码:

const issueList = [
  "Ajout de google analytics",
  "Déployement en continue dev",
  "Déployement en continue préproduction",
  "Déployement en continue production",
  "Déployment ipa automatique sur app store",
  "Déployment apk automatique sur play store"
];

const existingIssueList = [
  "Déployement en continue production",
  "Déployement en continue préproduction",
  "List swipe web"
];

for (let i = 0; i < issueList.length; i += 1) {
  let issueFound = null;
  for (let j = 0; j < existingIssueList.length; j += 1) {
    if (issueList[i] === existingIssueList[j]) {
      issueFound = existingIssueList[j];
      break;
    }
  }
  
  // If issue was found, we do not need to create theissue
  let issue;
  if (issueFound) {
    issue = updateIssue({
      issue: issueFound,
    });
    console.log(`issue has been updated`);
  } else {
    issue = createIssue({
      issue: issueList[i],
    });
    console.log(`issue has been created`);
  }
}

function updateIssue(){}
function createIssue(){}

我希望找到重复的值并更新它们,我在哪里失败了?

【问题讨论】:

  • 会发生什么?请添加想要的结果。
  • 如果您必须经常在长数组中查找字符串,请改用Set,因为它查找速度很快。

标签: javascript arrays node.js compare


【解决方案1】:

您的逻辑似乎很复杂,请检查您是否可以使用以下逻辑:

    for (let i = 0; i < issueList.length; i += 1) {
       if(existingIssueList.indexOf(issueList[i]) > -1) {
          //Issue already exists
       }else {
         //Create Issue 
       }
    }

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    lodash 可能会有所帮助,例如

    _.filter(issueList, (val, i, iteratee) => _.includes(iteratee, val, i + 1));
    

    【讨论】:

      【解决方案3】:

      出于可读性原因,我添加了includes。记录创建/更新了哪个问题后,您的代码似乎可以正常工作:

      const issueList = [
        "Requetes de création de table commerçant",
        "Déployment apk automatique sur play store"
      ];
      
      const existingIssueList = [
        "Déployment apk automatique sur play store",
        "Déployment ipa automatique sur app store",
        "Déployement en continue production",
        "Déployement en continue préproduction"
      ];
      
      for (let i = 0; i < issueList.length; i += 1) {
        let issueFound = existingIssueList.includes(issueList[i]);
        
        // If issue was found, we do not need to create theissue
        let issue;
        if (issueFound) {
          issue = updateIssue({
            issue: issueFound,
          });
          console.log(`issue has been updated: ` + issueList[i]);
        } else {
          issue = createIssue({
            issue: issueList[i],
          });
          console.log(`issue has been created: ` + issueList[i]);
        }
      }
      
      function updateIssue(){}
      function createIssue(){}

      【讨论】:

        【解决方案4】:

        const issueList = [
          "Déployement en continue dev",
          "Déployement en continue préproduction",
          "Déployement en continue production",
          "Déployment ipa automatique sur app store",
          "Déployment apk automatique sur play store"
        ];
        
        const existingIssueList = [
          "Déployment apk automatique sur play store",
          "Déployment ipa automatique sur app store"
        ];
        
        let issueFoundIndex = issueList.forEach(issue => {
            const existingIssueListIndex = existingIssueList.findIndex(x => x === issue);
        
            if(existingIssueListIndex !== -1){
              console.log('Found issue:' + existingIssueList[existingIssueListIndex]);
            }        
        
            const mutatedIssue = existingIssueListIndex !== -1
              ? updateIssue(existingIssueList[existingIssueListIndex])
              : createIssue(issue);
            console.log(mutatedIssue);
        });
        
        function updateIssue(issue){ return issue; }
        function createIssue(issue){ return issue; }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-05
          • 2019-05-04
          • 1970-01-01
          • 1970-01-01
          • 2013-02-02
          • 1970-01-01
          • 2013-08-02
          相关资源
          最近更新 更多