0

I have two a

var statusFilter = [
  {label: '1', checked: false},
  {label: '3', checked: false},
  {label: '4', checked: false},
  {label: '5', checked: false},
];

var activeStatusFilter = [
  {label: '1', checked: true},
  {label: '2', checked: true},
];

I need to filter out activeStatusFilter such that any element in activeStatusFilter not present in statusFilter should be removed. So the result of the function should be [{label: '1', checked: true}] because label: '2' is not present in status filter.

I have written a function to do the same but it always return an empty array.

const z = activeStatusFilter.filter( activeStatus =>
{
  let found = false;
  statusFilter.forEach(status => {
    console.log( activeStatus );
    console.log( "status", status )
    found =  ( activeStatus.label === status.label )
    return found
  })
  return found;
} )

I feel that in the forEach when found = ( activeStatus.label === status.label ) it should return from the forEach but it does not. Can someone help me in why this is not returning or a more optimised approach

leo
  • 103
  • 1
  • 10
  • Does this answer? https://stackoverflow.com/questions/43555904/foreach-for-in-not-returning-values . – Harmandeep Singh Kalsi Jun 29 '20 at 13:37
  • Does this answer your question? [forEach/for...in not returning values?](https://stackoverflow.com/questions/43555904/foreach-for-in-not-returning-values) – ABGR Jun 29 '20 at 13:57

6 Answers6

1

You can do it with a Set:

const labels = new Set(statusFilter.map(v => v.label));
const filtered = activeStatusFilter.filter(v => labels.has(v.label));
Aplet123
  • 33,825
  • 1
  • 29
  • 55
1

You can achieve the desired output by a combinatino of filter and find

const z = activeStatusFilter.filter( activeStatus => {
  return statusFilter.find( s => activeStatus.label === s.label)
})
Dhruv Shah
  • 1,611
  • 1
  • 8
  • 22
1
var statusFilter = [
  {label: '1', checked: false},
  {label: '3', checked: false},
  {label: '4', checked: false},
  {label: '5', checked: false},
];

var activeStatusFilter = [
  {label: '1', checked: true},
  {label: '2', checked: true},
];

const result = activeStatusFilter.filter(x => {
       return (statusFilter.findIndex( y => y.label === x.label) > -1)
})
Raffobaffo
  • 2,806
  • 9
  • 22
1

Here you go !

var statusFilter = [
  {label: '1', checked: false},
  {label: '3', checked: false},
  {label: '4', checked: false},
  {label: '5', checked: false},
];

var activeStatusFilter = [
  {label: '1', checked: true},
  {label: '2', checked: true},
];
var set = new Set();
statusFilter.forEach(status =>  set.add(status.label));

activeStatusFilter =activeStatusFilter.filter(active => set.has(active.label));

console.log(activeStatusFilter);
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
1

var statusFilter = [
  {label: '1', checked: false},
  {label: '3', checked: false},
  {label: '4', checked: false},
  {label: '5', checked: false},
];

var activeStatusFilter = [
  {label: '1', checked: true},
  {label: '2', checked: true},
];
res=activeStatusFilter.filter(x=>statusFilter.some(y=>y.label==x.label))
console.log(res)
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
1

You need:

  1. filter()
  2. some()

an alternative approach to Set. Here is a working example:

var statusFilter = [
  {label: '1', checked: false},
  {label: '3', checked: false},
  {label: '4', checked: false},
  {label: '5', checked: false},
];

var activeStatusFilter = [
  {label: '1', checked: true},
  {label: '2', checked: true},
];

var result = activeStatusFilter.filter(k=>statusFilter.some(l=>l.label==k.label));

console.log(result);
gorak
  • 5,233
  • 1
  • 7
  • 19