使用下划线替换或推送数组中的对象

replace or push object in array using underscore

我有一个包含新数据的对象和一个包含已保存数据的数组。如果来自 newData 的 cnName 已经存在于 cnData 数组的 savedData 中,那么 savedData 中的对象应该替换为 newData.cnData[0] 对象。如果它不存在,那么数据应该被推送到 savedData

中的 cnData 数组
newData = {
  "cnGroupName": "cnGroupName1",
  "cnData": [{
    "cnName": "cn3",
    "data": {
       "color": "blue",
       "size": "42",
       "position": "right"
    }
  }]
}

savedData = [{
  "cnGroupName": "cnGroupName1",
  "cnData": [{
    "cnName": "cn1",
    "data": {
      "color": "red",
      "size": "42",
      "position": "right"
    }
  }, {
    "cnName": "cn2",
    "data": {
      "color": "blue",
      "size": "11",
      "position": "top"
    }
  }]
}]

我正在使用下划线

if(savedData.length){
    _.each(savedData, function(num, i){

        if(savedData[i].cnGroupName == newData.cnGroupName ){

            _.each(savedData[i].cnData, function(num, x){


               if(savedData[i].cnData[x].cnName == newData.cnData[0].cnName){

                    // the cnName already exists in savedData so replace
                    savedData[i].cnData[x] = newData.cnData[0] 


               }else{ 
                    // the cnName  does NOT exist in savedData so push the new object to savedData
                   savedData[i].cnData.push(newData.cnData[0])
                }
            })
        }else{
            // the cnGroupName from newData is not in saveData so push newData
            savedData.push(newData)
        }
    })
}

这不起作用的原因是因为在第二个 _.each() 语句中。如果 cnName 在 savedData 中不存在,它将为已在 savedData 中的每个对象推送一次 newData。因此,如果 savedData 中已经有 2 个保存的对象,那么 newData 将被推送 2 次

你可以在这个 plunker 中看到控制台日志,新数据被推送了两次

http://plnkr.co/edit/GkBoe0F65BCEYiFuig57?p=preview

我的 2 个问题是

  1. 是否有更简洁的下划线方式?
  2. 有没有办法阻止 _.each() 继续循环?

您是否考虑过在第二个 _.each 循环 newData.cnData

if(savedData.length > 0){

    _.each(savedData, function(num, i){

        if(savedData[i].cnGroupName == newData.cnGroupName ){

            _.each(newData.cnData, function(num, x){
           // ^ change the subject here


               if(savedData[i].cnData[x].cnName == newData.cnData[0].cnName){

                    // the cnName already exists in savedData so replace
                    console.log("cn names are the same")
                    savedData[i].cnData[x] = newData.cnData[0] 


               }else{ 
                    // the cnName  does NOT exist in savedData so push
                    console.log("cn names are diffrent")
                   savedData[i].cnData.push(newData.cnData[0])
                }
            })
        }else{
            // the cnGroupName from newData is not in saveData so push newData
            savedData.push(newData)
        }
    })
}

您可以使用 findIndex 找到第一个匹配的索引,如果找不到则更新。

if(savedData.length){
    var cnindex,
    index = _.findIndex(savedData, function (item) { return item.cnGroupName == newData.cnGroupName });
    if (index === -1)) {
        savedData.push(newData);
    } else {
        cnindex = _.findIndex(savedData[index].cnData, function (item) {return item.cnName == newData.cnData[0].cnName});
        if (cnindex === -1) {
            savedData[index].cnData.push(newData.cnData[0]);
        } else {
            savedData[index].cnData[cnindex] = newData.cnData[0];
        }
    }
}

注意:要在 try catch 块中停止每次迭代并在所需的断点处抛出自定义错误