使用JavaScript中的重复数组成员从JSON中删除元素

时间:2016-03-31 18:06:53

标签: javascript jquery arrays json duplicates

是否有一种简单的方法可以删除带有重复"数组成员的JSON元素"?

这是我的示例JSON对象:

{
"temperature": [
  {"datetime":"2011-01-27T11:40:50.000Z", "value":15},
  {"datetime":"2011-01-27T11:40:50.000Z", "value":16}, <-- this one should be removed
  {"datetime":"2011-01-27T11:41:00.000Z", "value":14},
  {"datetime":"2011-01-27T11:41:10.000Z", "value":15},
  {"datetime":"2011-01-27T11:41:10.000Z", "value":15}, <-- this one should be removed
  {"datetime":"2011-01-27T11:41:10.000Z", "value":14}, <-- this one should be removed
  {"datetime":"2011-01-27T11:41:20.000Z", "value":16}
  ]
}

至于我的例子,我想删除整个JSON数组,如果&#34; datetime&#34;是任何其他&#34; datetime&#34;的复制品。在其他JSON数组中。

我试过这样做,但它给了我看似相反的结果:

var datetimes = [];
for(var i = 0; i < obj.temperature.length; i++) {
  if($.inArray(obj.temperature[i].datetime, datetimes)) {
    obj.temperature.splice(i,1);
  }else {
    datetimes.push(obj.temperature[i].datetime);
  }
}

试试here

4 个答案:

答案 0 :(得分:1)

您可以稍微欺骗它,并将项目作为属性添加到对象中,并将日期作为属性键。这使它独一无二,然后你要做的就是从属性值重新创建数组。像这样:

var dict={}
obj.temperature.forEach(function(t) {
  dict[t.datetime]=t;
});
var arr=[];
for(var datetime in dict) {
  arr.push(dict[datetime]);
}

在这里小提琴:https://jsfiddle.net/cmfanjox/

答案 1 :(得分:1)

var i = 1

你从0开始问,这是x对象吗?它是什么。所以,它被拼接了。当您到达要删除的输入时,您已拼接出您想要保留的所有输入。

var datetimes = [];
for(var i = 1; i < obj.temperature.length; i++) {
  if($.inArray(obj.temperature[i].datetime, datetimes)) {
    obj.temperature.splice(i,1);
  }else {
    datetimes.push(obj.temperature[i].datetime);
  }
}

答案 2 :(得分:0)

看一下这个片段:

var items = [{
  "datetime": "2011-01-27T11:40:50.000Z",
  "value": 15
}, {
  "datetime": "2011-01-27T11:40:50.000Z",
  "value": 16
}, {
  "datetime": "2011-01-27T11:41:00.000Z",
  "value": 14
}, {
  "datetime": "2011-01-27T11:41:10.000Z",
  "value": 15
}, {
  "datetime": "2011-01-27T11:41:10.000Z",
  "value": 15
}, {
  "datetime": "2011-01-27T11:41:10.000Z",
  "value": 14
}, {
  "datetime": "2011-01-27T11:41:20.000Z",
  "value": 16
}];

var dedup = items.reduce(function(a, b) {
  function indexOfProperty(a, b) {
    for (var i = 0; i < a.length; i++) {
      if (a[i].value == b.value && a[i].datetime == b.datetime) {
        return i;
      }
    }
    return -1;
  }

  if (indexOfProperty(a, b) < 0) a.push(b);
  return a;
}, []);

答案 3 :(得分:0)

嗯,有一个没有jQuery的简单解决方案:

var obj = {
"temperature": [
  {"datetime":"2011-01-27T11:40:50.000Z", "value":15},
  {"datetime":"2011-01-27T11:40:50.000Z", "value":16},
  {"datetime":"2011-01-27T11:41:00.000Z", "value":14},
  {"datetime":"2011-01-27T11:41:10.000Z", "value":15},
  {"datetime":"2011-01-27T11:41:10.000Z", "value":15},
  {"datetime":"2011-01-27T11:41:10.000Z", "value":14},  
  {"datetime":"2011-01-27T11:41:20.000Z", "value":16}
 ]
};

var temp = [];

for (var i = 0; i < obj.temperature.length; i++) {
    if (temp.indexOf(obj.temperature[i].datetime) === -1) {
        temp.push(obj.temperature[i].datetime);
        obj.temperature.splice(i + 1, 1)
    };
};

console.log(obj);