[英]Compare array of objects with array of string and remove duplicates
I have been searching for quite some time, but cannot find the solution.我一直在寻找相当长的时间,但找不到解决方案。
I have an array of objects -我有一组对象 -
const arrayOfObjects = [{id:"EUR", name:"EURO"}, {id:"USD", name:"US Dollar"}...]
and then I have an array of strings然后我有一个字符串数组
const arrayOfStrings = ["EUR"]
I want to get a third array, which will contain complete objects, but only the ones where id!== an element in arrayOfStrings.我想获得第三个数组,它将包含完整的对象,但只包含 id!== arrayOfStrings 中的元素的数组。
So the result -所以结果——
const resultArray = [{id:"USD", name:"US Dollar"}].
How would I go about that?我该怎么做?
EDIT: I am doing this as part of a createSelector编辑:我这样做是作为 createSelector 的一部分
export const currencies = curr => {
return createSelector(
[arrayObjectSelector, arrayStringsSelector(curr)],
(objectArr, stringArr) =>
objectArr &&
objectArr.filter(
currency => stringArr && stringArr.indexOf(currency.id) == -1
)
);
};
For some reason, the currencies array remains unchanged..出于某种原因,货币数组保持不变..
You could filter the array using Array#filter
with non-exist id value of arrayOfStrings
您可以使用
Array#filter
和不存在的 id 值arrayOfStrings
过滤数组
const arrayOfObjects = [{id:"EUR", name:"EURO"}, {id:"USD", name:"US Dollar"}] const arrayOfStrings = ["EUR"] const res = arrayOfObjects.filter(item=> arrayOfStrings.indexOf(item.id) == -1); console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.