-3

I have two arrays and I want to find matching elements of these arrays

const array1 = [{"id": 0, "value": 1}, {"id": 1, "value": 2}, {"id": 2, "value": 3}, {"id": 3, "value": 4}, {"id": 4, "value": 5}, {"id": 5, "value": 6}, {"id": 6, "value": 7}, {"id": 7, "value": 8}, {"id": 8, "value": 9}, {"id": 9, "value": 10}]
const array2 = [0, 1, 11, 12]

how can I find matching elements of array1 and array2?

my expected output is

const array3 = [0, 1]
10
  • There will be no matching elements since array1 contains objects, and array2 contains strings. Commented Jan 18, 2021 at 9:52
  • 2
    Does this answer your question? Check if an array contains any element of another array in JavaScript Commented Jan 18, 2021 at 9:52
  • 2
    @Shivam In the future, when asking a question, you should provide your inputs (which you have), your expected output (missing), and most importantly, the code you have tried so far to accomplish your goal + links to other posts that didn't work for you (also missing). If you edit your question to include these, some of the dvs might get removed Commented Jan 18, 2021 at 10:03
  • 1
    @NickParsons I will follow that. Commented Jan 18, 2021 at 10:05
  • 1
    I'm fairly your question is a dupe of this though: How to find array in array Commented Jan 18, 2021 at 10:07

3 Answers 3

3

You should make use of Array.prototype.filter() together with Array.prototype.some():

const array1 = [{"id": 0, "value": 1}, {"id": 1, "value": 2}, {"id": 2, "value": 3}, {"id": 3, "value": 4}, {"id": 4, "value": 5}, {"id": 5, "value": 6}, {"id": 6, "value": 7}, {"id": 7, "value": 8}, {"id": 8, "value": 9}, {"id": 9, "value": 10}],
      array2 = [0, 1],
      
      
      result = array2.filter(_id => array1.some(({id}) => id === _id))
      
console.log(result)

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

Comments

2

By mapping you can find your array like this

let array2 = ['0', '1']
let found = false;  
array1.map(ele =>{
   let found = (ele.id.toString() === array2[0].toString() && 
                ele.value.toString() === array2[1].toString())? true : false;
})
console.log(found)

Comments

2

if i understood corretly here is how you can filter your array1 if you want to filter on value use value.toString()

function myFn() {
  let array1 = [{"id": 0, "value": 1}, {"id": 1, "value": 2}, {"id": 2, "value": 3}, {"id": 3, "value": 4}, {"id": 4, "value": 5}, {"id": 5, "value": 6}, {"id": 6, "value": 7}, {"id": 7, "value": 8}, {"id": 8, "value": 9}, {"id": 9, "value": 10}]
  let array2 = ['0', '1','11','12']

  let idArray = array1.map(a => (a.id));
  let filteredArray=idArray.filter(value=>array2.includes(value.toString()));
  console.log(filteredArray);
  
}

myFn();

4 Comments

I am expecting my result array as [0,1].
array2 is already [0, 1] then what would be the relation with array1
if my array2 is [0, 1, 11, 12] then?
@Shivam see the edited answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.