代码之家  ›  专栏  ›  技术社区  ›  user3142695

如何计算第二位置相同值的两个数组的差

  •  0
  • user3142695  · 技术社区  · 4 年前

    我将一个git diff输出字符串解析到这个数组中,它将在一个包中提供所有更改的依赖项。json文件:

    const data = [
        [ '-', '@date-io/moment', '1.3.13', '1', '3', '13' ],
        [ '+', '@date-io/moment', '1.3.14', '1', '3', '14' ],
        [ '-', '@emotion/react', '11.7.0', '11', '7', '0' ],
        [ '-', '@emotion/styled', '11.6.0', '11', '6', '0' ],
        [ '+', '@emotion/react', '11.8.2', '11', '8', '2' ],
        [ '+', '@emotion/styled', '11.8.1', '11', '8', '1' ]
    ]
    

    第一个要素( + / - )显示是否添加或删除了此更改。第二个元素是包名(这是连接两个数组的元素),接下来的字段给出了版本。 现在我需要迭代这个数组,以找出每个包都进行了哪种版本更改。所以输出应该是:

    {
        '@date-io/moment': 'patch',
        '@emotion/react': 'minor',
        '@emotion/styled': 'minor'
    }
    

    我试图通过这样做来获得所有的包裹

    const result = {}
    data.forEach(d => {
        result[d[1]] = bump // how to calculate `bump`?
    })
    

    但这不管用 + - 它不计算版本凹凸的类型。

    3 回复  |  直到 4 年前
        1
  •  3
  •   Asplund Ransh Anand    4 年前

    使用 Array.prototype.reduce 要减少,可以将数组减少为一个对象,并针对每个包使用 Array.prototype.find 找到相应的变化。然后,您可以比较版本并确定其变化。

    const data = [
        [ '-', '@date-io/moment', '1.3.13', '1', '3', '13' ],
        [ '+', '@date-io/moment', '1.3.14', '1', '3', '14' ],
        [ '-', '@emotion/react', '11.7.0', '11', '7', '0' ],
        [ '-', '@emotion/styled', '11.6.0', '11', '6', '0' ],
        [ '+', '@emotion/react', '11.8.2', '11', '8', '2' ],
        [ '+', '@emotion/styled', '11.8.1', '11', '8', '1' ]
    ];
    
    const changes = data.reduce((t, [type, name, , major, minor, patch], i, arr) => {
        if (type === "+") {
            const match = arr
              .find(([type, pName]) => type === "-" && pName === name);
            if (!match) {
              return t; // handle no match here
            }
            const [, , , pMajor, pMinor, pPatch] = match;
            return {
                ...t,
                [name]: pMajor !== major ? "major" :
                    pMinor !== minor ? "minor" :
                    "patch"
            }
        }
        return t;
    }, {});
    
    console.log(changes);
        2
  •  2
  •   Som Shekhar Mukherjee    4 年前
    • 创建一个 Map 在所有减法中
    • 循环所有添加的内容,并使用贴图将其减少到所需的对象。

    const data = [
      ["-", "@date-io/moment", "1.3.13", "1", "3", "13"],
      ["+", "@date-io/moment", "1.3.14", "1", "3", "14"],
      ["-", "@emotion/react", "11.7.0", "11", "7", "0"],
      ["-", "@emotion/styled", "11.6.0", "11", "6", "0"],
      ["+", "@emotion/react", "11.8.2", "11", "8", "2"],
      ["+", "@emotion/styled", "11.8.1", "11", "8", "1"],
    ];
    
    const subMap = new Map(data.filter((d) => d[0] === "-").map((d) => [d[1], d]));
    
    const additions = data.filter((d) => d[0] === "+");
    
    const updates = additions.reduce((updates, addition) => {
      const pkgName = addition[1];
      const [maj, min, patch] = addition.slice(-3);
      const [oldMaj, oldMin, oldPatch] = subMap.get(pkgName).slice(-3);
    
      if (maj === oldMaj && min === oldMin && patch >= oldPatch) {
        updates[pkgName] = "patch";
      } else if (maj === oldMaj && min >= oldMin) {
        updates[pkgName] = "minor";
      } else {
        updates[pkgName] = "major";
      }
    
      return updates;
    }, {});
    
    console.log(updates);
        3
  •  0
  •   Jatin Parmar    4 年前

    首先,让我们在两个不同的对象中分离+和-如下所示

    let added={},removed={};
    for(let i=0;i<data.length;i++){
       if(data[i][0]=='-'){
         removed[data[i][1]]={
          version:data[i][2],
           major:data[i][3],
          min:data[i][4],
           patch:data[i][5]
        }
      }else{
      added[data[i][1]]={
      version:data[i][2],
      major:data[i][3],
      min:data[i][4],
      patch:data[i][5]
         }
      }
    }
    

    然后创建一个函数get diff string,如下所示

    function getPackageChanges(ad,rm){
     if(ad.major!==rm.major) return "major";
     if(ad.min!==rm.min) return "minor";
     if(ad.patch!==rm.patch)return "patch";
     return "unknown";
    }
    

    然后,您可以通过这种方式从其键添加映射,以获得结果字符串

    let res={};
    Object.keys(added).forEach(t=>{
    
    if(removed[t]!==undefined){
      res[t]=getPackageChanges(removed[t],added[t])
    }
    
    })
    

    const data = [
        [ '-', '@date-io/moment', '1.3.13', '1', '3', '13' ],
        [ '+', '@date-io/moment', '1.3.14', '1', '3', '14' ],
        [ '-', '@emotion/react', '11.7.0', '11', '7', '0' ],
        [ '-', '@emotion/styled', '11.6.0', '11', '6', '0' ],
        [ '+', '@emotion/react', '11.8.2', '11', '8', '2' ],
        [ '+', '@emotion/styled', '11.8.1', '11', '8', '1' ]
    ]
    
    
        let added={},removed={};
        for(let i=0;i<data.length;i++){
           if(data[i][0]=='-'){
             removed[data[i][1]]={
              version:data[i][2],
               major:data[i][3],
              min:data[i][4],
               patch:data[i][5]
            }
          }else{
          added[data[i][1]]={
          version:data[i][2],
          major:data[i][3],
          min:data[i][4],
          patch:data[i][5]
             }
          }
        }
    
        function getPackageChanges(ad,rm){
         if(ad.major!==rm.major) return "major";
         if(ad.min!==rm.min) return "minor";
         if(ad.patch!==rm.patch)return "patch";
         return "unknown";
        }
    
    
        let res ={};
        Object.keys(added).forEach(t=>{
    
        if(removed[t]!==undefined){
          res[t]=getPackageChanges(removed[t],added[t])
        }
    
        })
    console.log("result",res)