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

时间:2022-05-24 07:37:39

Is there a simple way to remove JSON elements with duplicate "array members"?

有没有一种简单的方法来删除具有重复“数组成员”的JSON元素?

This is my example JSON object:

这是我的示例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}
  ]
}

As for my example I want to remove the whole JSON array if "datetime" is a duplicate of any other "datetime" inside other JSON arrays.

至于我的例子,如果“datetime”与其他JSON数组中的任何其他“datetime”重复,我想删除整个JSON数组。

I tried doing it this way but it gave me the seemingly opposite result:

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

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

Try it here

试试吧

4 个解决方案

#1


1  

You can cheat it a little and add the items as properties to an object, with the date as the property key. This makes it unique and then all you have to do is recreate the array from the property values. Something like this:

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

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

Fiddle here: https://jsfiddle.net/cmfanjox/

小提琴:https://jsfiddle.net/cmfanjox/

#2


1  

var i = 1

var i = 1

You start at zero and ask, is x object here? Which it is. So, it gets spliced. By the time you get to the input you want to remove you have spliced out all the input that you wanted to keep.

你从零开始问,这是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);
  }
}

#3


0  

Take a look at this snippet:

看一下这个片段:

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;
}, []);

#4


0  

Well, there is one simple solution without jQuery:

嗯,有一个没有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);

#1


1  

You can cheat it a little and add the items as properties to an object, with the date as the property key. This makes it unique and then all you have to do is recreate the array from the property values. Something like this:

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

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

Fiddle here: https://jsfiddle.net/cmfanjox/

小提琴:https://jsfiddle.net/cmfanjox/

#2


1  

var i = 1

var i = 1

You start at zero and ask, is x object here? Which it is. So, it gets spliced. By the time you get to the input you want to remove you have spliced out all the input that you wanted to keep.

你从零开始问,这是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);
  }
}

#3


0  

Take a look at this snippet:

看一下这个片段:

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;
}, []);

#4


0  

Well, there is one simple solution without jQuery:

嗯,有一个没有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);