0

Below I have an array of objects

var data = [{
  "time": "1572024707.4763825",
  "rssi": "32",
  "id": "77777"
}, {
  "time": "1572024709.0991757",
  "rssi": "32",
  "id": "77777"
}, {
  "time": "1572024704.4570136",
  "rssi": "32",
  "id": "555555"
}, {
  "time": "1572024708.3903246",
  "rssi": "32",
  "id": "77777"
}, {
  "time": "1572024699.7132683",
  "rssi": "32",
  "id": "66666"
}]

How can I restructure it to remove the repeating id's with the oldest time

I tried to pull all the unique IDs from the array so I can loop through the data array but then the code started to get too long.

  data.forEach(item => {

    IDs.push(item.id);
  });

  var unqIDs = [...new Set(IDs)];
  console.log(unqIDs);

the output should look like this

outPutShouldBe = [{
  "time": "1572024699.7132683",
  "rssi": "32",
  "id": "66666"
},{
  "time": "1572024709.0991757",
  "rssi": "32",
  "id": "77777"
},  {"time": "1572024704.4570136",
  "rssi": "32",
  "id": "555555"
}
]
Shubham Verma
  • 4,918
  • 1
  • 9
  • 22
Jay
  • 67
  • 9

5 Answers5

4

Using forEach() find() filter() and filter() to decide which element to return

var data = [{"time": "1572024707.4763825","rssi": "32","id": "77777"},{"time": "1572024709.0991757","rssi": "32","id": "77777"}, {"time": "1572024704.4570136","rssi": "32","id": "555555"}, {"time": "1572024708.3903246","rssi": "32","id": "77777"}, {"time": "1572024699.7132683","rssi": "32","id": "66666"}]

let resultsArray = []

data.forEach(obj=>{
  const foundObj = resultsArray.find(data => data.id === obj.id)

  if(foundObj && new Date(foundObj.time) > new Date(obj.time)){
     const filteredArray = resultsArray.filter(data => data.id === obj.id)
     resultsArray = [...filteredArray , foundObj]
  } else if (!foundObj){
     resultsArray.push(obj)
  }
})

console.log(resultsArray)
Willman.Codes
  • 1,352
  • 1
  • 5
  • 13
4

Create an object mapping ids to the item w/ the earliest time of those with that id:

var keydata = {};
data.forEach(item=>{ 
    var p = keydata[item.id]; 
    if ( !p || p.time>item.time ) { 
        keydata[item.id] = item; 
    }});

Now gather up the values in that object:

var newdata = [];
for ( var k in keydata ) { 
    newdata.push(keydata[k]);
}

or the more elegant (thanks, @TulioF.):

var newdata = Object.values(keydata)
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • For your second step, you could do `Object.values(keydata)` to get the dictionary values. – Tolio Oct 25 '19 at 20:14
2

You coud take an object as hash table and get the values directly.

var data = [{ time: "1572024707.4763825", rssi: "32", id: "77777" }, { time: "1572024709.0991757", rssi: "32", id: "77777" }, { time: "1572024704.4570136", rssi: "32", id: "555555" }, { time: "1572024708.3903246", rssi: "32", id: "77777" }, { time: "1572024699.7132683", rssi: "32", id: "66666" }],
    result = Object.values(data.reduce((r, o) => {
        if (!r[o.id] || +r[o.id].time > +o.time) r[o.id] = o;
        return r;
    }, {}));

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Here you can do something like this :

let existMap = {};
data.filter(val => {
  if((val.id in existMap) && (val.time>existMap[val.id])) return;
  else{
    existMap[val.id] = val.time;
    return true;
  }
})
console.log(result)

The condition can be changed based on requirement. just want to reference for your problem.

Shubham Verma
  • 4,918
  • 1
  • 9
  • 22
0

use lodash to sort the array in descending order or ascending order as per your need (desc, asc) and get the zeroth object. try something like this. filter and orderBy

var data = [{
  "time": "1572024707.4763825",
  "rssi": "32",
  "id": "77777"
}, ....];

let idsSet = new Set();

data.map(item=> idsSet.add(item.id));

let idsArr = Array.from(idsSet);

let newArr = [];
idsArr.map(id=>{
let tempArray = data.filter(item => item.id === id);
return newArr.push((_.orderBy(tempArray, ['time'],['desc']))[0]);
} )

console.log(newArr);

console output

[ {
  "time": "1572024709.0991757",
  "rssi": "32",
  "id": "77777"
}, {
  "time": "1572024704.4570136",
  "rssi": "32",
  "id": "555555"
},  {
  "time": "1572024699.7132683",
  "rssi": "32",
  "id": "66666"
}];
blueseal
  • 2,726
  • 6
  • 26
  • 41