I have a array of javascript objects with some key and value. Below is how my array looks like.
[
{
"timestamp": 1474328370007,
"message": "hello"
},
{
"timestamp": 1474328302520,
"message": "how are you"
},
{
"timestamp": 1474328370007,
"message": "hello"
},
{
"timestamp": 1474328370007,
"message": "hello"
}
]
I want to remove the duplicate occurring of timestamp in the object and keep only single occurring of that object. The matching should happen based on the timestamp and not the message.
expected output is
[
{
"timestamp": 1474328302520,
"message": "how are you"
},
{
"timestamp": 1474328370007,
"message": "hello"
}
]
trying something like this
var fs = require('fs');
fs.readFile("file.json", 'utf8', function (err,data) {
if (err) console.log(err);;
console.log(data);
// var result = [];
for (i=0; i<data.length;i++) {
if(data[i].timestamp != data[i+1].timestamp)
console.log('yes');
}
});
I cannot figure out the data[i+1]
part after the array ends. Is there any easy way with which I can do the above deduplication?
thank you in advance