-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]
Shivam
  • 2,147
  • 1
  • 11
  • 28
  • There will be no matching elements since array1 contains objects, and array2 contains strings. – Or Assayag Jan 18 '21 at 09:52
  • 2
    Does this answer your question? [Check if an array contains any element of another array in JavaScript](https://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-element-of-another-array-in-javascript) – emerson.marini Jan 18 '21 at 09:52
  • The principle should still apply to your case. You'll obviously need to tweak the code. – emerson.marini Jan 18 '21 at 09:56
  • Also, your tags should be changed. This is unrelated to either `reactjs` or `react-native`. – emerson.marini Jan 18 '21 at 09:56
  • From what we can see, you want to know how to match elements between two arrays. Isn't that what you're looking for? – emerson.marini Jan 18 '21 at 09:58
  • Your code shows only very basic Javascript. The tags should reflect that. – emerson.marini Jan 18 '21 at 09:59
  • 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 – Nick Parsons Jan 18 '21 at 10:03
  • 1
    @NickParsons I will follow that. – Shivam Jan 18 '21 at 10:05
  • 1
    I'm fairly your question is a dupe of this though: [How to find array in array](https://stackoverflow.com/a/65717198) – Nick Parsons Jan 18 '21 at 10:07
  • @Shivam your desired output is unclear since you haven't explained the question clearly enough (see all the answers that misunderstood your question) or provided your expected output in the question... – Nick Parsons Jan 18 '21 at 10:20

3 Answers3

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)
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
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)
Sanoodia
  • 830
  • 4
  • 9
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();
manikant gautam
  • 3,521
  • 1
  • 17
  • 27