【问题标题】:How to replace data of an array if some value match [duplicate]如果某些值匹配,如何替换数组的数据[重复]
【发布时间】:2021-06-08 21:31:22
【问题描述】:

我有这个数组,如果有一些重合,我想用 array2 的数据替换 array1 中的数据,例如从 array1 替换 {"Dates":"05/25/2021","valor ":"0"} array2 {Dates: "05/25/2021", count: "20"} 匹配的数据

array1 = [{Dates:"05/10/2021",count:"0"},{Dates:"05/11/2021",count:"0"},{Dates:"05/12/2021",count:"0"},{Dates:"05/13/2021",count:"0"},{Dates:"05/14/2021",count:"0"},{Dates:"05/15/2021",count:"0"},{Dates:"05/16/2021",count:"0"},{Dates:"05/17/2021",count:"0"},{Dates:"05/18/2021",count:"0"},{Dates:"05/19/2021",count:"0"},{Dates:"05/20/2021",count:"0"},{Dates:"05/21/2021",count:"0"},{Dates:"05/22/2021",count:"0"},{Dates:"05/23/2021",count:"0"},{Dates:"05/24/2021",count:"0"},{Dates:"05/25/2021",count:"0"},{Dates:"05/26/2021",count:"0"},{Dates:"05/27/2021",count:"0"},{Dates:"05/28/2021",count:"0"},{Dates:"05/29/2021",count:"0"},{Dates:"05/30/2021",count:"0"},{Dates:"05/31/2021",count:"0"},{Dates:"06/01/2021",count:"0"},{Dates:"06/02/2021",count:"0"},{Dates:"06/03/2021",count:"0"},{Dates:"06/04/2021",count:"0"},{Dates:"06/05/2021",count:"0"},{Dates:"06/06/2021",count:"0"},{Dates:"06/07/2021",count:"0"},{Dates:"06/08/2021",count:"0"}];

array2 = [{Dates: "05/25/2021", count: "20"},{Dates: "05/26/2021", count: "0"},{Dates: "05/27/2021", count: "3"},{Dates: "05/28/2021", count: "10"},{Dates: "06/07/2021", count: "0"}];

如果有人可以帮助我,我将不胜感激,谢谢

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    使用Array.findArray.forEach 的组合,您可以遍历array2 并更新array1 中的匹配对象。

    array2.forEach(elm => {
      let match = array1.find(x => {
        return x.Dates == elm.Dates
      });
      if(typeof(match) != "undefined")
        match.count = elm.count;
    });
    

    完整的sn-p:

    array1 = [{"Dates":"05/10/2021",count:"0"},{"Dates":"05/11/2021",count:"0"},{"Dates":"05/12/2021",count:"0"},{"Dates":"05/13/2021",count:"0"},{"Dates":"05/14/2021",count:"0"},{"Dates":"05/15/2021",count:"0"},{"Dates":"05/16/2021",count:"0"},{"Dates":"05/17/2021",count:"0"},{"Dates":"05/18/2021",count:"0"},{"Dates":"05/19/2021",count:"0"},{"Dates":"05/20/2021",count:"0"},{"Dates":"05/21/2021",count:"0"},{"Dates":"05/22/2021",count:"0"},{"Dates":"05/23/2021",count:"0"},{"Dates":"05/24/2021",count:"0"},{"Dates":"05/25/2021",count:"0"},{"Dates":"05/26/2021",count:"0"},{"Dates":"05/27/2021",count:"0"},{"Dates":"05/28/2021",count:"0"},{"Dates":"05/29/2021",count:"0"},{"Dates":"05/30/2021",count:"0"},{"Dates":"05/31/2021",count:"0"},{"Dates":"06/01/2021",count:"0"},{"Dates":"06/02/2021",count:"0"},{"Dates":"06/03/2021",count:"0"},{"Dates":"06/04/2021",count:"0"},{"Dates":"06/05/2021",count:"0"},{"Dates":"06/06/2021",count:"0"},{"Dates":"06/07/2021",count:"0"},{"Dates":"06/08/2021",count:"0"}];
    
    array2 = [{Dates: "05/25/2021", count: "20"},{Dates: "05/26/2021", count: "0"},{Dates: "05/27/2021", count: "3"},{Dates: "05/28/2021", count: "10"},{Dates: "06/07/2021", count: "0"}];
    
    array2.forEach(elm => {
      let match = array1.find(x => {
        return x.Dates == elm.Dates
      });
      if(typeof(match) != "undefined")
        match.count = elm.count;
    });
    
    console.log(array1);

    【讨论】:

    • forEach 扩展方法比array2 上的map 更合适。我建议在设置 count 属性之前验证 array1.find 返回值。目前尚不清楚array2 日期是否保证在array1 中。
    • 很公平。我已经更新了答案。
    • 非常感谢你们
    【解决方案2】:

    作为一种天真的方法,我会这样做,这就是你要找的吗?

    for(let i = 0; i < array1.length; i++) {
       for(let j = 0; j < array2.length; j++) {
          if(array1[i].Dates == array2[j].Dates)
             array1[i] = array2[j];
       }
    }
    

    【讨论】:

    • 是的,两个答案都对我有很大帮助,非常感谢
    【解决方案3】:

    一种只对每个数组进行一次迭代的方法是使用 Dates 作为键从 array2 创建一个 Map,然后使用 Object.assign() 映射第一个数组。

    如果 assign 方法中的 get() 未定义(未存储此类日期),它将忽略未定义并返回 array1 对象的原始值。

    这避免了使用 find() 或嵌套 for 循环等方法的所有额外数组循环

    const a2Map = new Map(array2.map(e => [e.Dates, e]))
    
    const res = array1.map(e => Object.assign({}, e, a2Map.get(e.Dates)));
    
    console.log(res);
    <script>
    array1 = [{"Dates":"05/10/2021",count:"0"},{"Dates":"05/11/2021",count:"0"},{"Dates":"05/12/2021",count:"0"},{"Dates":"05/13/2021",count:"0"},{"Dates":"05/14/2021",count:"0"},{"Dates":"05/15/2021",count:"0"},{"Dates":"05/16/2021",count:"0"},{"Dates":"05/17/2021",count:"0"},{"Dates":"05/18/2021",count:"0"},{"Dates":"05/19/2021",count:"0"},{"Dates":"05/20/2021",count:"0"},{"Dates":"05/21/2021",count:"0"},{"Dates":"05/22/2021",count:"0"},{"Dates":"05/23/2021",count:"0"},{"Dates":"05/24/2021",count:"0"},{"Dates":"05/25/2021",count:"0"},{"Dates":"05/26/2021",count:"0"},{"Dates":"05/27/2021",count:"0"},{"Dates":"05/28/2021",count:"0"},{"Dates":"05/29/2021",count:"0"},{"Dates":"05/30/2021",count:"0"},{"Dates":"05/31/2021",count:"0"},{"Dates":"06/01/2021",count:"0"},{"Dates":"06/02/2021",count:"0"},{"Dates":"06/03/2021",count:"0"},{"Dates":"06/04/2021",count:"0"},{"Dates":"06/05/2021",count:"0"},{"Dates":"06/06/2021",count:"0"},{"Dates":"06/07/2021",count:"0"},{"Dates":"06/08/2021",count:"0"}];
    
    array2 = [{Dates: "05/25/2021", count: "20"},{Dates: "05/26/2021", count: "0"},{Dates: "05/27/2021", count: "3"},{Dates: "05/28/2021", count: "10"},{Dates: "06/07/2021", count: "0"}];
    
    </script>

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 2018-11-13
      • 2018-04-15
      • 2014-10-09
      • 2022-01-15
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      相关资源
      最近更新 更多