0

Hello guys i've tried to filter like this for result like this using filter and includes.. but not work, any solution using includes or something?

companies ids
[1,2,3]
user companies ids
[1,2]
filtered result
[1,2]

i've tried like these

this.company.filter(company => company.id.includes(this.reviewerData.company_ids))

but output justlike []

thanks guys

1
  • includes function can't accept a array as parameter Commented Dec 21, 2020 at 6:20

1 Answer 1

2

let companiesids = [1,2,3];
let userids =  [1,2];

let results = companiesids.filter(f => userids.indexOf(f) > -1);
console.log(results);

UPDATED:

let companiesids = [{id: 1, name: 'a'},{id: 2, name: 'b'},{id: 3, name: 'c'}];
let userids =  [1,2];

let results = companiesids.filter(f => userids.indexOf(f.id) > -1);
console.clear();
console.log(results);

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

3 Comments

ithink thats work, but i have company like this company{id:1, name:"balala"} usercompanyids: [1,2]
Do a map for company first, and then do filter. company.map(m => m.id)
if i mapping.. how can i display name?

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.