I have an object which I am getting from the DB, You can see the object which is coming from the DB This is what i’am getting from the DB.
JavaScript
x
42
42
1
[{
2
id:1,
3
cust_fname: "rock",
4
cust_no:"C001",
5
cust_need: "urgent",
6
cust_place: "poland",
7
date_created: "2021-04-16 18:50:40.658+00",
8
},
9
{
10
id:1,
11
cust_fname: "rock",
12
cust_no:"C001",
13
cust_need: "not-urgent",
14
cust_place: "poland",
15
date_created: "2021-04-16 19:50:40.658+00"
16
},
17
{
18
id:2,
19
cust_fname: "rmbo",
20
cust_no:"C002",
21
cust_need: "not-urgent",
22
cust_place: "England",
23
date_created: "2021-04-16 18:50:40.658+00"
24
},
25
{
26
id:3,
27
cust_fname: "ram",
28
cust_no:"C004",
29
cust_need: "urgent",
30
cust_place: "USA",
31
date_created: "2021-04-16 18:50:40.658+00"
32
},
33
{
34
id:3,
35
cust_fname: "ram",
36
cust_no:"C004",
37
cust_need: "not-urgent",
38
cust_place: "USA",
39
date_created: "2021-04-16 20:50:40.658+00"
40
}
41
]
42
I want to modify the above object such that the it should only return latest inserted vlaue if the array of object has two same object with the same id along with all the other object.
just posting the object output which I want for more clarity.
removing the object with id:1 which has date_created: “2021-04-16 18:50:40.658+00”;
JavaScript
1
26
26
1
[{
2
id:1,
3
cust_fname: "rock",
4
cust_no:"C001",
5
cust_need: "not-urgent",
6
cust_place: "poland",
7
date_created: "2021-04-16 19:50:40.658+00"
8
},
9
{
10
id:2,
11
cust_fname: "rmbo",
12
cust_no:"C002",
13
cust_need: "not-urgent",
14
cust_place: "England",
15
date_created: "2021-04-16 18:50:40.658+00"
16
},
17
{
18
id:3,
19
cust_fname: "ram",
20
cust_no:"C004",
21
cust_need: "urgent",
22
cust_place: "USA",
23
date_created: "2021-04-16 20:50:40.658+00"
24
}
25
]
26
Looking for solution how i can modify this obj or any other way also be aprriciated.
Advertisement
Answer
Here is a sort and a filter
You could alternatively use a Set.
JavaScript
1
14
14
1
const data = [{ id:1, cust_fname: "rock", cust_no:"C001", cust_need: "urgent", cust_place: "poland", date_created: "2021-04-16 18:50:40.658+00", }, { id:1, cust_fname: "rock", cust_no:"C001", cust_need: "not-urgent", cust_place: "poland", date_created: "2021-04-16 19:50:40.658+00" }, { id:2, cust_fname: "rmbo", cust_no:"C002", cust_need: "not-urgent", cust_place: "England", date_created: "2021-04-16 18:50:40.658+00" }, { id:3, cust_fname: "ram", cust_no:"C004", cust_need: "urgent", cust_place: "USA", date_created: "2021-04-16 18:50:40.658+00" }, { id:3, cust_fname: "ram", cust_no:"C004", cust_need: "not-urgent", cust_place: "USA", date_created: "2021-04-16 20:50:40.658+00" } ];
2
3
let newData = data.slice(0); // copy the data
4
newData = newData.sort((a,b) => {
5
if (a.id===b.id) { // if same ID sort date in descending order
6
if (b.date_created > a.date_created) return 1;
7
if (a.date_created > b.date_created) return -1
8
}
9
else return (a.id < b.id) ? -1 : 1; // else sort in ascending order
10
}).filter(({id},i) => { // filter on unique IDs
11
if (i>0) return id!==newData[i-1].id
12
return true;
13
})
14
console.log(newData)