简体   繁体   中英

how to filter an array based on dynamic values in javascript

i want to filter the given below array on the basis of dynamic values (name and section).

var allResponse = [
{
  "name": "ABC",
  "class": "8",
  "section": "A",
},
{
  "name": "DEF",
  "class": "7",
  "section": "B",
},
{
  "name": "ABC",
  "class": "8",
  "section": "D",
},
]

These values of name and section , i'm getting on Click of check box. On basis of these value , i want to filter this given array allResponse . Values can be multiple like: Filter the array where name is ABC and section is A and D ,

Like after getting value i tried to create an object to filter like given below to filter iteratively. But not able to do it

var filterLiterals= [{"name": "ABC"},{"section": "A"},{"section": "D"}];
or
var filterLiterals= ["ABC","A","D"];

So result should be

{
  "name": "ABC",
  "class": "8",
  "section": "A",
},

I'm confused which approach should follow to get the result.

I tried it by using || and && operator but not able to get the result what i want.

Could anybody please suggest me to suitable and proper approach to filter this array. I'm not able to do it.

Is it possible to filter in this way in javascript in any way...?

You can create a function which will accept the name and an array of section. Inside the function use filter and in the call back function return those object whose name matches and the section is included in the array of section passed to the function

 var allResponse = [{ "name": "ABC", "class": "8", "section": "A", }, { "name": "DEF", "class": "7", "section": "B", }, { "name": "ABC", "class": "8", "section": "D", } ] function filterArray(name, sectionArray) { return allResponse.filter(function(elem) { return elem.name = name && sectionArray.includes(elem.section) }) }; console.log(filterArray('ABC', ['A', 'D']))

If I'm reading your question correctly you want to filter your data based on what you clicked.

 var allResponse = [ { "name": "ABC", "class": "8", "section": "A", }, { "name": "DEF", "class": "7", "section": "B", }, { "name": "ABC", "class": "8", "section": "D", }, ]; var clickedValue = "ABC"; var filteredResponse = allResponse.filter( function(response) { return response.name === clickedValue }); console.log(filteredResponse);

I think this should resolve your filtering problem.

In my understanding, I need to find elements satisfying below,

name: ABC section: A or D

 var allResponse = [ { "name": "ABC", "class": "8", "section": "A", }, { "name": "DEF", "class": "7", "section": "B", }, { "name": "ABC", "class": "8", "section": "D", }, ]; var filterLiterals = [ {"name": "ABC"}, {"section": "A"}, {"section": "D"} ]; const parsed = filterLiterals.reduce((acc, obj) => { const key = Object.keys(obj)[0]; if (typeof acc[key] === 'undefined') acc[key] = [obj[key]]; else acc[key].push(obj[key]); return acc; }, {}); //console.log("parsed: ", parsed); var answer = allResponse.filter(obj => { let flag = true; for (const key of Object.keys(parsed)) { if (!parsed[key].includes(obj[key])) { flag = false; break; } } return flag; }); console.log(answer);

Run this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM