mrvdrdg
mrvdrdg

Reputation: 31

How filter object values with es6

i have a 2 object which i wan't to make filtering with es6

first is my data object and second selected some data.

I wan't to get all items in data object which have second object values

let data = [
  {
    id: 1,
    name: 'A',
    status: 1
  },
  {
    id: 2,
    name: 'B',
    status: 1
  },
  {
    id: 3,
    name: 'C',
    status: 3
  },
  {
    id: 4,
    name: 'D',
    status: 2
  }
]

and second object is :

let selectedStatus = [
  {
    id: 1,
    status: 1
  },
  {
    status: 3
  }
]

in this case i want't to get data object items which contains same statuses in second object so in this case i need to get this result:

data = [
  {
    id: 1,
    name: 'A',
    status: 1
  },
  {
    id: 2,
    name: 'B',
    status: 1
  },
  {
    id: 3,
    name: 'C',
    status: 3
  },
]

Upvotes: 3

Views: 275

Answers (5)

lirumlarum
lirumlarum

Reputation: 13

If you want to generate a third list, like your expected result, you can generate a white-list to match against.

const whiteList = selectedStatus.map((sel) => sel.status); // which gives you an array of all selected status you want to filter for

const filteredData = data.filter((data) => ~whiteList.indexOf(data.status)); // please consider that filter returns a new array that contain all items where the condition was falsy. That can be confusing.

to understand the ~ operator please check https://wsvincent.com/javascript-tilde/

you may struggle with the result of the filtered array. Please consider that array.filter returns a new array that contain all items which did NOT met the condition in other words its a negation. That can be confusing. https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

ES7

const filteredData = data.filter((data) => whiteList.includes(data.status));

Upvotes: 0

Henrique Zago
Henrique Zago

Reputation: 51

let result = [];    
selectedStatus.forEach(selectedStatus => result = result.concat(data.filter(status => status.status === selectedStatus.status)))

Upvotes: 0

wentjun
wentjun

Reputation: 42566

Assuming you do not have any browser restrictions, you can make use of followed by using Array.includes() to check statuses on data which are on selectedStatus, followed by Array.filter() to filter out objects which match the required condition.

const data = [
  {
    id: 1,
    name: 'A',
    status: 1
  },
  {
    id: 2,
    name: 'B',
    status: 1
  },
  {
    id: 3,
    name: 'C',
    status: 3
  },
  {
    id: 4,
    name: 'D',
    status: 2
  }
]

const selectedStatus = [
  {
    id: 1,
    status: 1
  },
  {
    status: 3
  }
];

const res = data.filter(obj => selectedStatus.map(s => s.status).includes(obj.status));

console.log(res);

Upvotes: 0

SuleymanSah
SuleymanSah

Reputation: 17888

You can do like this:

data = data.filter(item =>
  selectedStatus.map(s => s.status).includes(item.status)
);

Upvotes: 1

Gokulakannan T
Gokulakannan T

Reputation: 614

Below snippet will give the expected answer:

var result = [];
data.forEach((value) => {
    selectedStatus.forEach(val => {
        if(value.status == val.status) { 
            result.push(value) 
        } 
    }); 
});
console.log(result)

Upvotes: 0

Related Questions