1
{  
"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"):

I am using the above function for filtering the data based on search string.

I want to filter the nested array of object keep the structure of the object same The output of the above function is below, where i am getting all object of a list instead of getting only one object which matches the search text

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


        ]
    }]
}

}

The expected Output is

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


        ]
    }]
}

}

1
  • May be you can used find method of loadsh.js which give you desired result. More details visit lodash.com/docs/4.17.4#find Commented Dec 21, 2017 at 4:47

2 Answers 2

1

You have 2 arrays, so you need to filter the first one then the second one :

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'));

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks , But when i search it is giving filtered data and when i clear the search, it is again giving filtered data instead of original data.
This is not related to the code above. Check some other parts or ask another question
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.