简体   繁体   English

如何根据javascript中的动态值过滤数组

[英]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. name 和 section 的这些值,我正在单击复选框。 On basis of these value , i want to filter this given array allResponse .基于这些值,我想过滤这个给定的数组allResponse Values can be multiple like: Filter the array where name is ABC and section is A and D ,值可以是多个,例如:过滤名称为 ABC 且部分为 A 和 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...?是否可以以任何方式在javascript中以这种方式过滤......?

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在函数内部使用filter并在回调函数中返回名称匹配且该section包含在传递给该函数的部分数组中的那些对象

 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名称:ABC 部分:A 或 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!运行这个!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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