1

I've searched a lot for this but couldn't find anything that match my requirement.

I want to remove all the duplicates but keeping the last entry not the first.

The array is already presorted I don't want to mess with sorting

So it looks like this :

[{
    name:"Joe",
    status:"foo1" },
  {
    name:"Joe",
    status:"foo2"},
  {
     name:"Vani",
    status:"foo5"
  }]

The expected output looks like:

  [{
    name:"Joe",
    status:"foo2"},
  {
     name:"Vani",
    status:"foo5"
  }]

I'd be thankful if someone can help me!

3
  • Perhaps try reversing the array, then try any other method that keeps the first value. You can then reverse it back Commented Mar 13, 2019 at 11:16
  • 1
    Welcome to Stack Overflow! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Mar 13, 2019 at 11:17
  • The array is already pre-sorted I don't want to mess with sorting! , I will add this above Commented Mar 13, 2019 at 11:18

5 Answers 5

3

You can simply use reduce

let arr = [{ name:"Joe", status:"foo1" }, { name:"Joe", status:"foo2"}, { name:"Vani", status:"foo5" }]

let op = arr.reduce((op,inp)=>{
  op[inp.name] = inp
  return op
},{})

console.log(Object.values(op))

Sign up to request clarification or add additional context in comments.

1 Comment

It is not guaranteed that values in array returned by Object.values() will preserve the same order as in input array.
2

You can make use of ES6 Map. As from Docs:

The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

const data = [
  { name:"Joe", status:"foo1" },
  { name:"Joe", status:"foo2" },
  { name:"Vani", status:"foo5" }
];

const removeDupes = (arr, map = new Map()) => {
  arr.forEach(o => map.set(o.name, o));

  return [...map.values()];
};

console.log(removeDupes(data));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

Try using simple forEach,

const arr = [{ name:"Joe", status:"foo1" }, { name:"Joe", status:"foo2"}, { 
    name:"Vani", status:"foo5" }];

let result = {};
arr.forEach((val) => { result[val.name] = val; });

console.log(Object.values(result));

Hope this helps...

Comments

0

The accepted answer doesn't actually keep the order, it amends the object at the initially found index

You could amend it to look like this:

const data = [
  { name:"Joe", status:"foo1" },
  { name:"Vani", status:"foo2" },
  { name:"Joe", status:"foo3" }
];

const removeDupes = (arr, map = new Map()) => {
  arr.forEach((o, i) => map.set(o.name, {...o, order: i}));

  return [...map.values()].sort((a, b) => a.order - b.order);
};

console.log(removeDupes(data));

Or perhaps do something more simple like:

const data = [
  { name:"Joe", status:"foo1" },
  { name:"Vani", status:"foo2" },
  { name:"Joe", status:"foo3" }
];

let newData = [];
data.forEach(x => {
  newData = newData.filter(y => y.name != x.name);
  newData.push(x);
});

console.log(newData);

I'll let someone else figure out a more performant solution...

Comments

0
// remove duplicate while preserve order
array.filter((row, i, arr) => arr.slice(0, i).findIndex((l) => l === row) === -1)
// change `l === row` to your own comparison logic

In your case:

const data = [
  {
    name:"Joe",
    status:"foo1"
  },
  {
    name:"Joe",
    status:"foo2"
  },
  {
    name:"Vani",
    status:"foo5"
  }
];

const duplicateRemoved = data
  .toReversed()
  .filter((row, i, arr) => arr.slice(0, i).findIndex((l) => l.name === row.name) === -1)
  .toReversed();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.