I have following array:
[
{name: "Joske", code: "IEDDK"},
{name: "Mieke", code: "IEDDK"},
{name: "Jan", code: "IEDDK"},
{name: "Test", code: "IsxJK"},
{name: "Koen", code: "IsxJK"},
]
And I need a function which gives me an array of objects where there is only once occurrence of the code property:
[
{name: "Joske", code: "IEDDK"},
{name: "Test", code: "IsxJK"},
]
I have thought about it and I think I'm making it harder than it needs to be. Does someone has a simple function to do this?
CodePudding user response:
If you don't mind a "last wins", this can be done nicely with
[...new Map(data.map(d => [d.code, d])).values()]
with O(n)
complexity (unlike some other approaches that re-iterate the array for every member, looking for matches thereby exhibiting quadratic complexity).
For a "first wins", just reverse your data:
[...new Map([...data].reverse().map(d => [d.code, d])).values()]
This can be folded into a typesafe generic function:
const distinctBy = <T, K>(data: T[], keySelector: (v: T) => K): T[] =>
[...new Map(data.map(d => [keySelector(d), d])).values()]
and used
distinctBy(data, d => d.code)
CodePudding user response:
You can try to solve it in a functional way by using array.reduce
:
let input = [
{name: "Joske", code: "IEDDK"},
{name: "Mieke", code: "IEDDK"},
{name: "Jan", code: "IEDDK"},
{name: "Test", code: "IsxJK"},
{name: "Koen", code: "IsxJK"},
];
let result = input.reduce((acc, cur) => {
if(acc.find(x => x.code === cur.code)){
return acc;
} else {
return [...acc, cur];
}
}, []);
console.log(result);
CodePudding user response:
You coud use a forEach
with find
method, this way:
let data = [ { name: "Joske", code: "IEDDK" }, { name: "Mieke", code: "IEDDK" },{ name: "Jan", code: "IEDDK" }, { name: "Test", code: "IsxJK" }, { name: "Koen", code: "IsxJK" }];
const removeSimilar = (array) => {
let result = [];
array.forEach((item) => {
if (!result.find((i) => i.code === item.code)) result.push(item);
});
return result;
};
console.log(removeSimilar(data))
CodePudding user response:
With filter
and findIndex
is also an option:
const arr = [
{name: "Joske", code: "IEDDK"},
{name: "Mieke", code: "IEDDK"},
{name: "Jan", code: "IEDDK"},
{name: "Test", code: "IsxJK"},
{name: "Koen", code: "IsxJK"},
];
const isTheSameAs = ({ code }) => item => item.code === code;
const filteredArr = arr
.filter((item, indexInOriginalArray, array) => array.findIndex(isTheSameAs(item)) === indexInOriginalArray);
console.log(filteredArr);
CodePudding user response:
Group your array based on code
using array#reduce
in an object accumulator and extract all the values using Object.values()
.
const data = [{ name: "Joske", code: "IEDDK" }, { name: "Mieke", code: "IEDDK" }, { name: "Jan", code: "IEDDK" }, { name: "Test", code: "IsxJK" }, { name: "Koen", code: "IsxJK" }, ],
result = Object.values(data.reduce((r, o) => {
r[o.code] = r[o.code] || o;
return r;
},{}));
console.log(result);