使用javascript中的搜索字符串过滤嵌套的对象数组

时间:2017-12-21 04:31:13

标签: javascript jquery arrays json lodash

{  
"rResponse":{  
    "rDetailsList":[  
        {  
            "rDate":"April 01, 2018",
            "rList":[  
                {  
                    "aName":"GOKQG C HQFUDHFPX",
                    "aNumber":"P3799838628"
                },
                {  
                    "aName":"IGNDPJR D EKYJYC",
                    "aNumber":"P3899820579"
                }
            ]
        },
        {  
            "rDate":"Jan 01, 2018",
            "rList":[  
                {  
                    "aName":"",
                    "aNumber":"A39A4035073"
                },
                {  
                    "aName":"YVTLW K SIGLC",
                    "aNumber":"A270M040558"
                }
            ]
        }
    ]
}

}

getFilteredResult(rDetails, searchText) {
                const regex = new RegExp(searchText, 'i');
                let result= rDetails.filter(a => 
                     a.rList.some(rItem=>
                    (rItem.aName.search(regex) > -1) ||
                            (rItem.aNumber.search(regex) > -1)  
                    ))
                console.log(result,"filteredResults")
                return result;
            }

let result=getFilteredResult(rResponse.rDetailsList, "A270M040558"):

我正在使用上述功能根据搜索字符串过滤数据。

我想过滤嵌套的对象数组,保持对象的结构相同 上面函数的输出如下,我得到列表的所有对象,而不是只获得一个与搜索文本匹配的对象

{
"rResponse": {
    "rDetailsList": [{
        "rDate": "Jan 01, 2018",
        "rList": [{
                "aName": "",
                "aNumber": "A39A4035073"
            },
            {
                "aName": "YVTLW K SIGLC",
                "aNumber": "A270M040558"
            }


        ]
    }]
}

}

预期的输出

{
"rResponse": {
    "rDetailsList": [{
        "rDate": "Jan 01, 2018",
        "rList": [
            {
                "aName": "YVTLW K SIGLC",
                "aNumber": "A270M040558"
            }


        ]
    }]
}

}

2 个答案:

答案 0 :(得分:1)

你有2个阵列,所以你需要过滤第一个,然后过滤第二个:



const rDetailsList = [  
        {  
            "rDate":"April 01, 2018",
            "rList":[  
                {  
                    "aName":"GOKQG C HQFUDHFPX",
                    "aNumber":"P3799838628"
                },
                {  
                    "aName":"IGNDPJR D EKYJYC",
                    "aNumber":"P3899820579"
                }
            ]
        },
        {  
            "rDate":"Jan 01, 2018",
            "rList":[  
                {  
                    "aName":"",
                    "aNumber":"A39A4035073"
                },
                {  
                    "aName":"YVTLW K SIGLC",
                    "aNumber":"A270M040558"
                }
            ]
        }
    ];

const myFilter = (arr, num) => {
  const rDetails = arr.filter(det => !!det.rList.find(l => l.aNumber === num));
  
  return rDetails.map(det => {
    det.rList = det.rList.filter(l => l.aNumber === num);
    return det;
  });
};

console.log(myFilter(rDetailsList, 'A270M040558'));




答案 1 :(得分:0)

 const res = _.chain(rDetailsList)
     .map(rDetail => _.assign( // iterate array and set filtered rList
         {}, // use new object to avoid mutations
         rDetail,
         { rList: _.filter(rDetail.rList, { aNumber: 'A270M040558' }) }
     ))
     .reject(rDetail => _.isEmpty(rDetail.rList)) // remove elements with empty rList
     .value();