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

SQL Query for Pagination

1  Asked on November 12, 2021 by abbas

     

Android. Invisible Top Bar

2  Asked on November 12, 2021 by sb69lg

   

loop does not iterate over list

1  Asked on November 12, 2021 by ripj

     

Grouping data by week

3  Asked on November 12, 2021 by beefstu

     

Does changing state cause re-render in ReactJS

1  Asked on November 12, 2021 by coderocks

     

Split a long string into full sentences in R

1  Asked on November 12, 2021 by jenifav

     

What is n= n ^1U<<i?

2  Asked on November 10, 2021 by akshayroy

       

What is the excel formula for within the next 7 business days?

2  Asked on November 10, 2021 by gradstud

 

Shuffle certain items in python array

3  Asked on November 10, 2021 by david-ferris

     

Ask a Question

Get help from others!

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