Home > OS >  Javasript, remove ALL, REALLY ALL duplicates from object
Javasript, remove ALL, REALLY ALL duplicates from object

Time:02-16

Have object with notes, time entries, want to remove ALL (ALL, sorry for repeating this part, I've went through many examples and they all remove second, third etc duplicate only, but they include first duplicate) notes with same time DUPLICATES from it.

Well I need to separate unique from duplicates, I've managed to get all duplicates from it with this code I found here on StackOverflow,

const allNotes = [
    {
        "note": 69,
        "time": 0
    },
    {
        "note": 57,
        "time": 0
    },
    {
        "note": 60,
        "time": 1.5
    },
    {
        "note": 64,
        "time": 2
    },
    {
        "note": 69,
        "time": 2.5
    },
    {
        "note": 71,
        "time": 3
    },
    {
        "note": 52,
        "time": 3
    },
    {
        "note": 64,
        "time": 4.5
    },
    {
        "note": 68,
        "time": 5
    },
    {
        "note": 71,
        "time": 5.5
    }
]

const getDuplicates = () => {
const values = allNotes;
const unique = new Object;
const lookup = values.reduce((a, e) => {
  a[e.time] =   a[e.time] || 0;
  return a;
}, {});
const duplicates = values.filter(e => lookup[e.time]);
console.log(duplicates);
}

And this code works like charm it produces

[
    {
        "note": 69,
        "time": 0
    },
    {
        "note": 57,
        "time": 0
    },
    {
        "note": 71,
        "time": 3
    },
    {
        "note": 52,
        "time": 3
    }
]

Still need to be able to get only unique time entries, need this result

[
    {
        "note": 60,
        "time": 1.5
    },
    {
        "note": 64,
        "time": 2
    },
    {
        "note": 69,
        "time": 2.5
    },
    {
        "note": 64,
        "time": 4.5
    },
    {
        "note": 68,
        "time": 5
    },
    {
        "note": 71,
        "time": 5.5
    }
]

CodePudding user response:

I guess you are looking for something like this:

array.filter(a => array.filter(b => a.time === b.time).length === 1)
// filter array and sort for duplicates, just keep element if time-property is unique

See a working example below:

const allNotes=[{note:69,time:0},{note:57,time:0},{note:60,time:1.5},{note:64,time:2},{note:69,time:2.5},{note:71,time:3},{note:52,time:3},{note:64,time:4.5},{note:68,time:5},{note:71,time:5.5}];

const res = allNotes.filter(a => allNotes.filter(b => a.time === b.time).length === 1);
console.log(res);

CodePudding user response:

You've already solved your problem since you're able to pick out the duplicates. Just filter the opposite.

const uniqueValues = values.filter(e => !lookup[e.time]);

CodePudding user response:

const _dedupArr: any[] = [];

allNotes.forEach((v, i) => {
    const find = _dedupArr.find(d => d.time == v.time);

    if(!find) {
        _dedupArr.push(v);
    }
});

console.log('_dedupArr', _dedupArr);

CodePudding user response:

Not a performant solution, but it definitely is an easy one... Convert every object into a primitive, these primitives can then be assigned to a map and only values can be retrieved.

const allNotes = [
    {
        "note": 69,
        "time": 0
    },
    {
        "note": 57,
        "time": 0
    },
    {
        "note": 60,
        "time": 1.5
    },
    {
        "note": 64,
        "time": 2
    },
    {
        "note": 69,
        "time": 2.5
    },
    {
        "note": 71,
        "time": 3
    },
    {
        "note": 52,
        "time": 3
    },
    {
        "note": 64,
        "time": 4.5
    },
    {
        "note": 68,
        "time": 5
    },
    {
        "note": 71,
        "time": 5.5
    },
];

const uniqueMap = new Map();
allNotes.forEach(value => uniqueMap.set(
  // Any conversion to primitive is fine, JSON.stringify too
  `${value.note}-${value.time}`,
  value,
));
console.log(Array.from(uniqueMap.values()));

  • Related