首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在多个条件下过滤对象的嵌套数组?

如何在多个条件下过滤对象的嵌套数组?
EN

Stack Overflow用户
提问于 2018-07-26 06:20:48
回答 4查看 3.8K关注 0票数 3

下面是对象的示例数组。我希望在criteriaTypeidsource的基础上过滤这个。如果没有匹配的input.source,父对象应该被过滤掉。此外,所有过滤条件都是可选的。

代码语言:javascript
复制
[{
    "id": "9be6c6299cca48f597fe71bc99c37b2f",
    "caption": "caption1",
    "criteriaType": "type2",
    "input": [
        {
            "id_1": "66be4486ffd3431eb60e6ea6326158fe",
            "criteriaId": "9be6c6299cca48f597fe71bc99c37b2f",
            "source": "type1",
        },
        {
            "id_1": "1ecdf410b3314865be2b52ca9b4c8539",
            "criteriaId": "9be6c6299cca48f597fe71bc99c37b2f",
            "source": "type2",
        }
    ]
},
{
    "id": "b83b3f081a7b45e087183740b12faf3a",
    "caption": "caption1",
    "criteriaType": "type1",
    "input": [
        {
            "id_1": "f46da7ffa859425e922bdbb701cfcf88",
            "criteriaId": "b83b3f081a7b45e087183740b12faf3a",
            "source": "type3",
        },
        {
            "id_1": "abb87219db254d108a1e0f774f88dfb6",
            "criteriaId": "b83b3f081a7b45e087183740b12faf3a",
            "source": "type1",
        }
    ]
},
{
    "id": "fe5b071a2d8a4a9da61bbd81b9271e31",
    "caption": "caption1",
    "criteriaType": "type1",
    "input": [
        {
            "id_1": "7ea1b85e4dbc44e8b37d1110b565a081",
            "criteriaId": "fe5b071a2d8a4a9da61bbd81b9271e31",
            "source": "type3",
        },
        {
            "id_1": "c5f943b61f674265b8237bb560cbed03",
            "criteriaId": "fe5b071a2d8a4a9da61bbd81b9271e31",
            "source": "type3",
        }
    ]
}]

我能够实现仅按criteriaType & id过滤。但是,如果没有匹配的input.source,我也不能通过source过滤来确保不返回父级。

代码语言:javascript
复制
var json = <<array of objects>> ;
const {objectId: id, ctype: criteriaType, inputSource: source } = param; // getting the the params
json = ctype ? json.filter(({criteriaType}) => criteriaType === ctype ): json;
json = (objectId ? json.filter(({id}) => id === objectId ): json)
       .map (({id, caption, criteriaType, input }) => {
         //some manipulation 
         return { //results after manipulation}
       })

帮帮我!提前谢谢。我不确定我们是否可以用链式过滤器来实现它。

寻找与esLint兼容的代码

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-07-26 06:58:01

requirements be是可选的,没有一个源匹配parent不应该返回https://jsfiddle.net/cpk18dt4/9/

注释在代码中。希望它能解释这个函数的作用。

代码语言:javascript
复制
const fnFilter = (criteriaType, id, source) => {
  let result = oData;

  if (criteriaType) { // it can be null (optional)
    result = result.filter(d => d.criteriaType === criteriaType);
  }
  if (id) { // it can be null (optional)
    result = result.filter(d => d.id === id);
  }
  if (source) { // it can be null (optional)
    result = result.filter(d => {
      const inputs = d.input.filter(inp => inp.source === source);

      // If none of the input.source match, the parent object should be filtered out
      if (inputs.length === 0) {
        return false;
      }
      d.input = inputs;
      return true;
    });
  }

  return result;
};
票数 1
EN

Stack Overflow用户

发布于 2018-07-26 06:24:49

有几种方法可以做到这一点。您可以在纯JS中实现这一点,我推荐使用Lodash

1) Lodash filters

代码语言:javascript
复制
``` javascript

var用户=[

{ 'user':'barney','age':36,'active':true },

{ 'user':'fred','age':40,'active':false }

];

_.filter(用户,函数(O){ return !o.active;});

// 'fred‘的=>对象

// _.matches迭代器速记。

_.filter(用户,{‘年龄’:36,‘活跃’:真});

// 'barney‘的=>对象

// _.matchesProperty迭代器速记。

_.filter(users,'active',false);

// 'fred‘的=>对象

// _.property迭代器速记。

_.filter(用户,‘活动’);

// 'barney‘的=>对象

代码语言:javascript
复制

2) JavaScript ES5 filter()

代码语言:javascript
复制
``` javascript

var word=‘喷雾’,‘极限’,‘精英’,‘旺盛’,‘毁灭’,‘现在’;

var result =单词

.filter(word => word.length > 6)

.filter(word => word.length < 8);

Console.log(结果);

//预期输出: Array "present“

代码语言:javascript
复制

2) MapReduce

MapReduce是我最喜欢的处理集合/集合的工具之一。

您在上面的代码中使用了map()。诀窍可能是将map更改为reduce

使用map,您可以获得1:1的收藏项进出比率。

使用reduce,您可以根据需要为输入中的每一项生成任意多的项。例如。

代码语言:javascript
复制
``` javascript

var stuff =‘沙发’,‘椅子’,‘桌子’;

var hasFiveLetters =stuff.reduce((总数,项目) => {

if (item.length === 5) total.push(item);//添加任何您喜欢的项目

return total;//别忘了返回total!

},[]);//将total初始化为[]

console.log(hasFiveLetters);//‘沙发’,‘椅子’;

代码语言:javascript
复制
票数 1
EN

Stack Overflow用户

发布于 2018-07-26 07:30:50

试试这个:

代码语言:javascript
复制
    var obj = [{
        "id": "9be6c6299cca48f597fe71bc99c37b2f",
        "caption": "caption1",
        "criteriaType": "type2",
        "input": [
            {
                "id_1": "66be4486ffd3431eb60e6ea6326158fe",
                "criteriaId": "9be6c6299cca48f597fe71bc99c37b2f",
                "source": "type1",
            },
            {
                "id_1": "1ecdf410b3314865be2b52ca9b4c8539",
                "criteriaId": "9be6c6299cca48f597fe71bc99c37b2f",
                "source": "type2",
            }
        ]
    },
    {
        "id": "b83b3f081a7b45e087183740b12faf3a",
        "caption": "caption1",
        "criteriaType": "type1",
        "input": [
            {
                "id_1": "f46da7ffa859425e922bdbb701cfcf88",
                "criteriaId": "b83b3f081a7b45e087183740b12faf3a",
                "source": "type3",
            },
            {
                "id_1": "abb87219db254d108a1e0f774f88dfb6",
                "criteriaId": "b83b3f081a7b45e087183740b12faf3a",
                "source": "type1",
            }
        ]
    },
    {
        "id": "fe5b071a2d8a4a9da61bbd81b9271e31",
        "caption": "caption1",
        "criteriaType": "type1",
        "input": [
            {
                "id_1": "7ea1b85e4dbc44e8b37d1110b565a081",
                "criteriaId": "fe5b071a2d8a4a9da61bbd81b9271e31",
                "source": "type3",
            },
            {
                "id_1": "c5f943b61f674265b8237bb560cbed03",
                "criteriaId": "fe5b071a2d8a4a9da61bbd81b9271e31",
                "source": "type3",
            }
        ]
    }];
    
      
    function filterObj(obj, column, value){
    	var newArray = obj.filter(function (el) {
      	    if(el[column]){
        	    return el[column] == value;
            }else{
        	    for(var key in el.input){
                    if(typeof(el.input[key] == "object")){
                        var item = el.input[key];
                        if(item[column] == value){return item;}
                    }
                }
            }
    	});
        return newArray;
    }
    
    console.log(filterObj(obj, 'caption','caption1'));
    console.log(filterObj(obj, 'criteriaId','fe5b071a2d8a4a9da61bbd81b9271e31'));
    console.log(filterObj(obj, 'id_1','1ecdf410b3314865be2b52ca9b4c8539'));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51528469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档