AnswerBun.com

How to filter array of objecs with an object

Stack Overflow Asked by Zkk on October 31, 2020

I want to filter an array of objects by items of another array. This is my list of items:

const list = ["first", "second", "third"]

This array can be empty, with one, two or three itens… By example:

const list = []
const list = ["first"]
let list = ["first", "third"]

So, I need to use my list to filter my myArr2 by status.

const myArr2 = [
  {
    id: "1",
    language: "portuguese",
    status: "first"    
  },
  {
    id: "2",
    language: "portuguese",
    status: "first"    
  },
  {
    id: "3",
    language: "portuguese",
    status: "second"    
  },
  {
    id: "4",
    language: "portuguese",
    status: "third"    
  },      
  {
    id: "5",
    language: "portuguese",
    status: "second"    
  },      
]

If my list is:

const list = [ "first", "third" ]

The output must be:

const myArr2 = [
  {
    id: "1",
    language: "portuguese",
    status: "first"    
  },
  {
    id: "2",
    language: "portuguese",
    status: "first"    
  },
  {
    id: "4",
    language: "portuguese",
    status: "third"    
  },      
]

If my list is:

const list = [ "first" ]

The output must be:

const myArr2 = [
  {
    id: "1"
    language: "portuguese"
    status: "first"    
  },
  {
    id: "2"
    language: "portuguese"
    status: "first"    
  }  
}

and so on…

I made this code but not works properly:

Object.entries(list).forEach(status => {
  myArr2.filter(item => {
    if(item.status === status){
      return item
    }
  })
})

What am I doing wrong?
Someone can help me?

3 Answers

You are doing it in what I will say reverse manner. You need to check for each item in myArr2 that whether the item has its status which is present in the list array. You can do it as follows -

Correct Code -

let myArr2 = [
  {
    id: "1",
    language: "portuguese",
    status: "first"    
  },
  {
    id: "2",
    language: "portuguese",
    status: "first"    
  },
  {
    id: "3",
    language: "portuguese",
    status: "second"    
  },
  {
    id: "4",
    language: "portuguese",
    status: "third"    
  },      
  {
    id: "5",
    language: "portuguese",
    status: "second"    
  },      
];
const list = [ "first", "third" ]

myArr2 = myArr2.filter(item => list.includes(item.status))
console.log(myArr2)


Mistake in your approach

Object.entries(list).forEach(status => {
  myArr2.filter(item => {
    if(item.status === status){
      return item
    }
  })
})

In the above approach, you will be iterating through the first element in the list (that is first ). So, it will filter out all the items from myArr2 which are not having status as first.

In the next iteration, you will check with second element in the list(that is second). So, now it will further again filter out all the items from myArr2 which do not have status as second. Since no elements have their status as both first and second at the same time, your code would probably return the resultant array as an empty array .


NOTE: you would need to assign the new array returned by myArr2 to some variable. The filter method doesn't filter out elements in-place. So, since you have not done that, your code would probably be giving you the origin array itself if you try checking it by console logging.

You can read more about filter() here .

Correct answer by Abhishek on October 31, 2020

You could filter the array. Check for this if the elements are part of your filterlist (indexOf != -1).

function filterArray(arr, list) {
    return arr.filter(el => {
        return list.indexOf(el.status)!=-1;
    });
}

const myArr2 = [
  {
    id: "1",
    language: "portuguese",
    status: "first"    
  },
  {
    id: "2",
    language: "portuguese",
    status: "first"    
  },
  {
    id: "3",
    language: "portuguese",
    status: "second"    
  },
  {
    id: "4",
    language: "portuguese",
    status: "third"    
  },      
  {
    id: "5",
    language: "portuguese",
    status: "second"    
  },      
]

list1 = []
list2 = ["first"]
list3 = ["first", "third"]

console.log(filterArray(myArr2, list3));
console.log(filterArray(myArr2, list2));
console.log(filterArray(myArr2, list1));

Answered by Sascha on October 31, 2020

You could destructure status and check if list contains this value.

result = myArr2.filter(({ status }) => list.includes(status));

Answered by Nina Scholz on October 31, 2020

Add your own answers!

Related Questions

How to fix dict to pandas dataframe

1  Asked on February 23, 2021 by spatial-digger

     

C Polynomial Addition exercise

3  Asked on February 23, 2021 by aaa

   

Fill a list with a for loop python

3  Asked on February 23, 2021 by dottorbooom

         

Sending ics file with MailKit and Stringbuilder

1  Asked on February 23, 2021 by nick-fleetwood

         

How to convert UINT16 into two signed chars?

1  Asked on February 22, 2021 by the-big-pescado

       

Understanding the increment in the array (v6++)[4] = v9;

2  Asked on February 22, 2021 by john-wick

 

Decrementing a number in c as an argument

1  Asked on February 22, 2021

 

$(ProjectName) and $(TargetName) MSBuild macros not working

2  Asked on February 22, 2021 by ivan-caravanio

     

Android: Using Microphone and Headphones Simultaneously

1  Asked on February 22, 2021 by poppy

     

How to print specific words from a sentence

1  Asked on February 22, 2021 by astronomical

 

getting parsing issue in GraphQL query

0  Asked on February 22, 2021 by sauribabu

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP