I have a multi dimentional array as following. I need to delete the previous row if the value of the particular key value duplicates
[
{"id":5, "name":"abc"}
{"id":5, "name":"abcd"}
{"id":6, "name":"abcde"}
]
I need to get the result as following after deleting the previous row if the value of id already exists.
[
{"id":5, "name":"abcd"}
{"id":6, "name":"abcde"}
]
Map can be leveraged to produce a pretty cool one-liner 😁
const input = [
{"id":5, "name":"abc"},
{"id":5, "name":"abcd"},
{"id":6, "name":"abcde"}
]
const output = [...new Map(input.map(o => [o.id, o])).values()]
console.log(output)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With