OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to compare elements in array objects using javascript

  • Thread starter Thread starter Virat
  • Start date Start date
V

Virat

Guest
I am new to javascript usage. I have a requirement to compare the field from two different array objects which are having list as below.

Code:
Array - A

[
  {
    "id": "xyz",
    "number": "123",
    "place": "Here",
    "phone": "9090909090"     
  },
  {
    "id": "abc",
    "number": "456",
    "place": "There",
    "phone": "9191919191"    
  },
 ]
 
 Array - B
 
 [
 {
    "element1" : "ert",
    "id1":"iii",
    "element2":"erws",
    "element3":"234"
    
 }
,
 {
    "element1" : "uio",
    "id1":"xyz",
    "element2":"puy",
    "element3":"090"
 }
]

The scenario is to compare for each 'id' in the list of array A, with the list of Array B to field 'id1'

Example -

I need to check from Array A -> 'id:xyz' matches the array B object field 'id1'. Array A - id: xyz should match with id1: xyz in Array B If the match occurs I need to pull out complete object from the array list A.

here id:xyz matches with id1:xyz

then pull out as below

Code:
[
  {
    "id": "xyz",
    "number": "123",
    "place": "Here",
    "phone": "9090909090"     
  }
 ]

Please help me with the suggestions to make this work using javascript.

<p>I am new to javascript usage.
I have a requirement to compare the field from two different array objects which are having list as below.</p>
<pre><code>Array - A

[
{
"id": "xyz",
"number": "123",
"place": "Here",
"phone": "9090909090"
},
{
"id": "abc",
"number": "456",
"place": "There",
"phone": "9191919191"
},
]

Array - B

[
{
"element1" : "ert",
"id1":"iii",
"element2":"erws",
"element3":"234"

}
,
{
"element1" : "uio",
"id1":"xyz",
"element2":"puy",
"element3":"090"
}
]
</code></pre>
<p>The scenario is to compare for each 'id' in the list of array A, with the list of Array B to field 'id1'</p>
<p>Example -</p>
<p>I need to check from Array A -> 'id:xyz' matches the array B object field 'id1'.
Array A - id: xyz should match with id1: xyz in Array B
If the match occurs I need to pull out complete object from the array list A.</p>
<p>here id:xyz matches with id1:xyz</p>
<p>then pull out as below</p>
<pre><code>[
{
"id": "xyz",
"number": "123",
"place": "Here",
"phone": "9090909090"
}
]
</code></pre>
<p>Please help me with the suggestions to make this work using javascript.</p>
 

Latest posts

Top