0

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

This is my example JSON object:

{
"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.

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 Answers 4

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/

Sign up to request clarification or add additional context in comments.

5 Comments

Can you provide a working jsfiddle? I can't get it to work.
sorry, your object was also named obj. Now it should work.
It works almost perfectly now, however it picks the last item not the first: jsfiddle.net/ra48f46c/2
Thanks alot, it works now, although it seems my error was that I should've set i to 1 and not to 0. I'll have to compare performance speed with your solution and Lea Fox's. I'll mark the fastest one as best answer I guess.
Okay, I've tested this further with much more data and it the end your solution was the only one that worked consistently. Thanks for the help!
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.

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

Comments

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

Comments

0

Well, there is one simple solution without 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.