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

Filter array of objects by comparing it with another array of objects

  • Thread starter Thread starter Aren Trot
  • Start date Start date
A

Aren Trot

Guest
I have an array of objects coming from backend as shown below.

Code:
let input  = [
    {month: "Dec", count: "45"}, 
    {month: "Mar", count: "12"}, 
    {month: "June", count: "5"} 
]

The below variable dropdownValues is the ones selected by user on frontend dropdown.

Code:
let dropdownValues = [ 
{key: 'Mar'}, 
{key: 'June'} 
]

so considering user selected Mar and June in dropdown, we want the input variable to only select those keys and compare it with month property of input and remove the other objects.

So the final output based on dropdownValues should look like this

Code:
output = [ 
    {month: "Mar", count: "12"}, 
    {month: "June", count: "5"} 
]

In order to achieve this result, i tried doing the following but it doesnot filter out Dec month object.

Code:
let output = input?.filter((el) => {
        return dropdownValues.some((f) => {
          return f.key === el.month;
        });
      });

can someone let me know where i am going wrong with this.

<p>I have an array of objects coming from backend as shown below.</p>
<pre><code>let input = [
{month: "Dec", count: "45"},
{month: "Mar", count: "12"},
{month: "June", count: "5"}
]
</code></pre>
<p>The below variable <code>dropdownValues</code> is the ones selected by user on frontend dropdown.</p>
<pre><code>let dropdownValues = [
{key: 'Mar'},
{key: 'June'}
]
</code></pre>
<p>so considering user selected <code>Mar</code> and <code>June</code> in dropdown, we want the <code>input</code> variable to only select those keys and compare it with <code>month</code> property of <code>input</code> and remove the other objects.</p>
<p>So the final output based on <code>dropdownValues</code> should look like this</p>
<pre><code>output = [
{month: "Mar", count: "12"},
{month: "June", count: "5"}
]
</code></pre>
<p>In order to achieve this result, i tried doing the following but it doesnot filter out <code>Dec</code> month object.</p>
<pre><code>let output = input?.filter((el) => {
return dropdownValues.some((f) => {
return f.key === el.month;
});
});
</code></pre>
<p>can someone let me know where i am going wrong with this.</p>
 

Latest posts

Top