I have an array
const obj=[{fullCart: 5, halfCart: 4, smu: 3, stowage: 5, index: "FC-093"},
{fullCart: 5, halfCart: 4, smu: 8, stowage: 5, index: "FC-093"},
{fullCart: 5, halfCart: 4, smu: 0, stowage: 5, index: "FC-093"},
{fullCart: 5, halfCart: 5, smu: 3, stowage: 5, index: "FC-093"},
{fullCart: 5, halfCart: 7, smu: 3, stowage: 5, index: "FC-093"}
]
I need to retain the last object in the array ie whichever is the previous object with same property here index:’FC-093′ should be removed. ie the result should be,
[{fullCart: 5, halfCart: 7, smu: 3, stowage: 5, index: "FC-093"}]
2
Answers
I am not entirely sure I fully understand your question. If you simply want to return the last entry you can do:
If you wanted to remove that last entry then you could make a new array and set the old one equal to the new one:
This is probably what you need.
unique
index value in an array using.map()
methodunique
array now and use thefindLast
method to get the last element in yourobj
array per value in yourunique
array and push it in yourresult
array