【问题标题】:compare two object array and push if not exist比较两个对象数组,如果不存在则推送
【发布时间】:2020-04-03 06:31:39
【问题描述】:

我有下面两个对象数组

var array_1 = [{"Date": "2020-04-01", "Item" : 001},{"Date": "2020-04-03", "Item" : 002}]
var base_array = [{"Date": "2020-04-01", "Item" : null}, {"Date": "2020-04-02", "Item" : null}, 
                  {"Date": "2020-04-04", "Item" : null}]

我打算修改array_1如下

 var array_1 = [{"Date": "2020-04-01", "Item" : 001},
                {"Date": "2020-04-02", "Item" : null},
                {"Date": "2020-04-03", "Item" : 002},
                {"Date": "2020-04-04", "Item" : null}]

由于 array_1 中不存在日期“2020-04-02”和“2020-04-04”,因此这些日期应该以“item”为空来推送,并且不应推送“2020-04-01”,因为它已经存在于array_1 中。

我已经尝试在每个循环之后执行,但无法继续

小提示:base_array 将始终包含比 array_1 更多的值。这就是为什么我使用 base_array 作为我的初始循环

$.each(base_array , function (key,bvalue) {

    $.each(array_1, function (key,value) {
          if(bvalue.Date != value.Date){

               array_1.push({"Date" : value.Date, "Item": value.Item})
          }
    })
})

【问题讨论】:

    标签: javascript jquery arrays object


    【解决方案1】:

    您可以在base_array 上使用Array.reduce(),将array_1 作为初始值传递,并且只添加来自base_array 的值,其中Date 尚不存在于进位值中:

    var array_1 = [{
      "Date": "2020-04-01",
      "Item": 001
    }, {
      "Date": "2020-04-03",
      "Item": 002
    }];
    var base_array = [{
        "Date": "2020-04-01",
        "Item": null
      }, {
        "Date": "2020-04-02",
        "Item": null
      },
      {
        "Date": "2020-04-04",
        "Item": null
      }
    ];
    
    array_1 = base_array.reduce((c, v) => c.concat(c.some(e => e.Date == v.Date) ? [] : [v]), array_1);
    console.log(array_1);

    【讨论】:

      【解决方案2】:

      您可以获取一个哈希表,将对象分配给相同的日期并获取一个对象数组。

      最后按Date对数组进行排序。

      var array_1 = [{ Date: "2020-04-01", Item: "001" }, { Date: "2020-04-03", Item: "002" }],
          base_array = [{ Date: "2020-04-01", Item: null }, { Date: "2020-04-02", Item: null }, { Date: "2020-04-04", Item: null }],
          result = Object
              .values([...base_array, ...array_1].reduce((r, o) => (r[o.Date] = o, r), {}))
              .sort(({ Date: a }, { Date: b }) => a > b || -(a < b))
      
      console.log(result);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        【解决方案3】:

        我不确定订单对您是否重要,但如果不重要,这里有一个解决方案

        // Keep track of all dates already present in `array_1`.
        let array_1_dates = new Set(array_1.map(array_1_elem => array_1_elem.Date));
        
        // Push elements from `base_array` that are not in `array_1`.
        for (let base_array_elem of base_array) {
          if (!array_1_dates.has(base_array_elem.Date)) {
            array_1.push(base_array_elem);
          }
        }
        

        【讨论】:

          【解决方案4】:

          首先使用filterbase_array 中删除array_1 上已经存在的元素。然后concat

          var array_1 = [{"Date": "2020-04-01", "Item" : 001},{"Date": "2020-04-03", "Item" : 002}]
          var base_array = [{"Date": "2020-04-01", "Item" : null}, {"Date": "2020-04-02", "Item" : null}, {"Date": "2020-04-04", "Item" : null}]
                            
          var filtered = base_array.filter(({Date}) => !array_1.some(el => el.Date == Date));
          var result = array_1.concat(filtered);
          console.log(result);

          【讨论】:

            【解决方案5】:

            您可以使用简单的循环并通过比较找到插入位置。

            var array_1 = [{
              "Date": "2020-04-01",
              "Item": 001
            }, {
              "Date": "2020-04-03",
              "Item": 002
            }]
            var base_array = [{
                "Date": "2020-04-01",
                "Item": null
              }, {
                "Date": "2020-04-02",
                "Item": null
              },
              {
                "Date": "2020-04-04",
                "Item": null
              }
            ]
            
            // iterate over second array
            base_array.forEach(el => {
              for (let i = 0; i < array_1.length; i++) {
                // if value found return
                if (array_1[i].Date === el.Date) return;
                // if position found, insert and return
                if (array_1[i].Date > el.Date) {
                  array_1.splice(i, 0, el);
                  return
                }
              }
              // or insert at the end of the array
              array_1.push(el);
            })
            
            console.log(array_1);

            注意: 假设第一个数组总是有序的。


            更新:或者如果顺序可能发生变化并且您不想在最后添加它们,那么您可以使用Array#someArray#filter 方法。

            var array_1 = [{
              "Date": "2020-04-01",
              "Item": 001
            }, {
              "Date": "2020-04-03",
              "Item": 002
            }]
            var base_array = [{
                "Date": "2020-04-01",
                "Item": null
              }, {
                "Date": "2020-04-02",
                "Item": null
              },
              {
                "Date": "2020-04-04",
                "Item": null
              }
            ]
            
            array_1.push(...base_array.filter(o => array_1.every(o1 => o1.Date !== o.Date)))
            
            console.log(array_1);

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-07-13
              • 2023-02-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多