Reputation: 20892
I've been struggling to filter an array of objects.
I have an array of objects like this:
const arrayOne = [
{userid: 111, photo: “blahblahlbha”}
{userid: 222, photo: “blahblahlbha”}
{userid: 333, photo: “blahblahlbha”}
{userid: 444, photo: “blahblahlbha”}
{userid: 555, photo: “blahblahlbha”}
]
And an array like this:
Const arrayTwo = [
111,
333
}
I want to create an updated arrayOne (arrayThree) like this (remove userid's found in arrayTwo)
const arrayThree = [
{userid: 222, photo: “blahblahlbha”}
{userid: 444, photo: “blahblahlbha”}
{userid: 555, photo: “blahblahlbha”}
]
I've tried a few variations of below without much luck:
const arrayThree = Object.keys(arrayOne).filter((k) => !arrayTwo.includes(arrayOne[k].userid))
What can I try next?
Upvotes: 1
Views: 74
Reputation: 725
Many used includes
method of Array which doesn't support in Internet Explorer so using indexOf
is a better option always.
const arrayOne = [
{userid: 111, photo: "blahblahlbha"},
{userid: 222, photo: "blahblahlbha"},
{userid: 333, photo: "blahblahlbha"},
{userid: 444, photo: "blahblahlbha"},
{userid: 555, photo: "blahblahlbha"},
]
const arrayTwo = [
111,
333
];
const arrayThree = arrayOne.filter(item => arrayTwo.indexOf(item.userid) < 0);
console.log(arrayThree)
Upvotes: 0
Reputation: 151
Try this.
const arrayOne = [
{userid: 111, photo: "blahblahlbha"},
{userid: 222, photo: "blahblahlbha"},
{userid: 333, photo: "blahblahlbha"},
{userid: 444, photo: "blahblahlbha"},
{userid: 555, photo: "blahblahlbha"},
]
const arrayTwo = [
111,
333
];
const arrayThree = arrayOne.filter(item => !arrayTwo.includes(item.userid));
Upvotes: 0
Reputation: 11001
Use filter
and includes
. (There are some typos in data, fixed as below)
const arrayOne = [
{ userid: 111, photo: "blahblahlbha" },
{ userid: 222, photo: "blahblahlbha" },
{ userid: 333, photo: "blahblahlbha" },
{ userid: 444, photo: "blahblahlbha" },
{ userid: 555, photo: "blahblahlbha" }
];
const arrayTwo = [111, 333];
const arrayThree = arrayOne.filter(({ userid }) => !arrayTwo.includes(userid));
console.log(arrayThree);
Upvotes: 1
Reputation: 29282
You can achieve the desired result using reduce
function. Iterate over arrayOne
, check if userid
of current object is present in arrayTwo
, if not present, add current object to new array that will be returned by reduce
function
const arrayOne = [
{userid: 111, photo: 'blahblahlbha'},
{userid: 222, photo: 'blahblahlbha'},
{userid: 333, photo: 'blahblahlbha'},
{userid: 444, photo: 'blahblahlbha'},
{userid: 555, photo: 'blahblahlbha'},
];
const arrayTwo = [
111,
333
];
const arrayThree = arrayOne.reduce((acc, curr) => {
if (!arrayTwo.includes(curr.userid)) {
acc.push(curr);
}
return acc;
}, []);
console.log(arrayThree);
Upvotes: 1
Reputation: 8558
You don't need Object.keys()
. Just use Array.filter()
And also there are some syntax errors in your code.
,
) between array items in arrayOne
. "
) are not actually quotation marks, you need
to convert them as well.arrayTwo
with square bracket (]
), not with
curly brace (}
).const arrayOne = [
{userid: 111, photo: "blahblahlbha"},
{userid: 222, photo: "blahblahlbha"},
{userid: 333, photo: "blahblahlbha"},
{userid: 444, photo: "blahblahlbha"},
{userid: 555, photo: "blahblahlbha"}
]
const arrayTwo = [
111,
333
]
const arrayThree = arrayOne.filter(item => !arrayTwo.includes(item.userid))
console.log(arrayThree)
Upvotes: 2