删除多维数组中的重复条目

时间:2014-08-08 09:13:23

标签: javascript arrays

我有一个像这样的json对象

var data = [
      {
        "id": 24,
        "title": "BCOM",
        "start": "2014-08-05 12:59:00 PM",
        "end": "2014-08-05 2:59:00 PM",
        "description": "mcom",
        "DIFF": 120
      },
      {
        "id": 26,
        "title": "MCOM",
        "start": "2014-08-10 12:59:00 PM",
        "end": "2014-08-10 4:59:00 PM",
        "description": "mcom",
        "DIFF": 240
      },
      {
        "id": 29,
        "title": "MCOM",
        "start": "2014-08-11 12:59:00 PM",
        "end": "2014-08-11 8:59:00 PM",
        "description": "mcom",
        "DIFF": 480
      },
      {
        "id": 30,
        "title": "MCOM",
        "start": "2014-08-13 12:59:00 PM",
        "end": "2014-08-13 4:59:00 PM",
        "description": "mcom",
        "DIFF": 240
      }
    ];

如果你在这里观察到mcom重复的标题,我想添加" DIFF"从数组如果相同的标题重复多次,我想通过javascript实现这一点。任何人请帮助我。

我的结果应该是

var data = [
      {
        "id": 24,
        "title": "BCOM",
        "start": "2014-08-05 12:59:00 PM",
        "end": "2014-08-05 2:59:00 PM",
        "description": "mcom",
        "DIFF": 120
      },
      {
        "id": 26,
        "title": "MCOM",
        "start": "2014-08-10 12:59:00 PM",
        "end": "2014-08-10 4:59:00 PM",
        "description": "mcom",
        "DIFF": 960
      },


    ];

由于

3 个答案:

答案 0 :(得分:2)

第一个解决方案(不安全)

var titles = [];
var indexes = [];
var index;
for (var i = 0; i < data.length; i++) {
    index = titles.indexOf(data[i].title);
    if(index == -1){
        titles.push(data[i].title);
        indexes.push(i);
    } else {
        data[indexes[index]].DIFF += data[i].DIFF;
        delete data[i];
    }
}

<强>解释

为了删除具有重复标题的项目,我们循环遍历数组的所有项目。我们在titles数组中存储新标题,并使用它来检查重复的标题。当我们存储标题时,我们还在indexes表中存储具有该标题的第一个项目的索引。当我们找到具有重复标题的项目时,我们将其DIFF添加到具有此标题的第一个元素,我们将其删除。

<强> Remarques

使用运算符delete是危险的,因为它会在数组中产生间隙。也许创建一个新数组来保存新项目会更安全。然后代码就像这样:

更安全的解决方案

var titles = [];
var uniquesData = [];
var index;
for (var i = 0; i < data.length; i++) {
    index = titles.indexOf(data[i].title);
    if(index == -1){
        titles.push(data[i].title);
        uniquesData.push(data[i]);
    } else {
        uniquesData[index].DIFF += data[i].DIFF;
    }
}
data = uniquesData;

答案 1 :(得分:1)

以下功能可以执行我认为您想要的功能。它会记住它访问的对象的标题,如果它遇到重复,则将 DIFF 添加到第一个实例的 DIFF ,然后从数组中删除副本。 / p>

它会修改原始数组,但可以更改它以便返回副本。

function removeDups(arr, prop) {

  // Object to store title of visited members
  var obj = {};
  var val;

  for (var i=0, iLen=arr.length; i<iLen; i++) {

    // Store a reference to the current member
    val = arr[i][prop];

    // If have a match
    if (obj.hasOwnProperty(val)) {

      // Add the DIFF property to the previous instance
      arr[obj[val]].DIFF += arr[i].DIFF;

      // Remove from array
      arr.splice(i, 1);

      // Decrement counter and limit to account for removed member
      --i;
      --iLen;

    // Otherwise, remember the property value and index of the member
    } else {
      obj[val] = i;
    }
  }
  return arr;
}

console.log(removeDups(data, 'title'));

答案 2 :(得分:1)

var indexCache = {};
var newData = [];
for (var i = 0; i < data.length; ++i) {
  var item = data[i];
  if (item.title in indexCache) {
    newData[indexCache[item.title]].DIFF += item.DIFF;
  }
  else {
    newData.push(item);
    indexCache[item.title] = newData.length - 1;
  }
}