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

时间:2015-05-18 19:39:24

标签: javascript arrays underscore.js

我有一个带有新数据的对象和一个已保存数据的数组。如果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()语句中。如果在savedData中不存在cnName,它将为已存在savedData的每个对象推送一次newData。因此,如果savedData中已存在2个已保存的对象,则newData将被推送2次

您可以在此plunker中看到控制台记录newData被按两次

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

我的2个问题是

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

2 个答案:

答案 0 :(得分:1)

您可以使用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块中停止每次迭代换行并在所需断点处抛出自定义错误

答案 1 :(得分:0)

您是否考虑过第二个newData.cnData上的_.each循环播放?

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)
        }
    })
}