简体   繁体   中英

How to filter the JSON array object like nested Sql Query using es6 syntax in effective way

I am new to ES6 syntax, please help me with this.

I have a JSON array like:

var data = [{
    "recordid": 1,
    "recordidclass": "Parent Class",
    "relatedrecid": 2,
    "relatedrecclass": "Child Class2"   
},
{
    "recordid": 1,
    "recordidclass": "Parent Class",
    "relatedrecid": 3,
    "relatedrecclass": "Child Class3"   
},
{
    "recordid": 9,
    "recordidclass": "Parent Class",
    "relatedrecid": 14,
    "relatedrecclass": "Child Class4"   
},
{
    "recordid": 2,
    "recordidclass": "Parent Class",
    "relatedrecid": 5,
    "relatedrecclass": "Child Class5"   
},
{
    "recordid": 5,
    "recordidclass": "Parent Class",
    "relatedrecid": 6,
    "relatedrecclass": "Child Class6"   
},
{
    "recordid": 3,
    "recordidclass": "Parent Class",
    "relatedrecid": 7,
    "relatedrecclass": "Child Class7"   
},
{
    "recordid": 4,
    "recordidclass": "Parent Class",
    "relatedrecid": 8,
    "relatedrecclass": "Child Class8"   
},{...}]

All the parent and child objects are in same array. I want to filter the data based on recordid and I should get all the object which matches with recordid and its relatedrecid object as well.

In short, based on the sample data, if I call a getRequiredData(1) (which is recordid ), I should get all the data which matches the recordid and its relatedrecid data as well.

My output should be:

var result = [{
    "recordid": 1,
    "recordidclass": "Parent Class",
    "relatedrecid": 2,
    "relatedrecclass": "Child Class2"   
},
{
    "recordid": 1,
    "recordidclass": "Parent Class",
    "relatedrecid": 3,
    "relatedrecclass": "Child Class3"   
},
{
    "recordid": 2,
    "recordidclass": "Parent Class",
    "relatedrecid": 5,
    "relatedrecclass": "Child Class5"   
},
{
    "recordid": 3,
    "recordidclass": "Parent Class",
    "relatedrecid": 7,
    "relatedrecclass": "Child Class7"   
}]

You may want to utilize a filter on the array. Info on that can be found here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Where you would do filteredResults = result.filter(function(item){return item.recordId == paramId});

Or good old fashioned for loop never fails.

And as Roman points out, es6 syntax lets you do this:

filteredResults = result.filter(item=>item.recordId == paramId);

which does the exact same thing, just slightly shorter hand syntax :)

Use array.find to locate the record with matching id. Use array.filter to locate records relating to the first one.

Here I have joined the results of those two operations into one array.

 var data = [{ "recordid": 1, "recordidclass": "Parent Class", "relatedrecid": 2, "relatedrecclass": "Child Class2" }, { "recordid": 1, "recordidclass": "Parent Class", "relatedrecid": 3, "relatedrecclass": "Child Class3" }, { "recordid": 9, "recordidclass": "Parent Class", "relatedrecid": 14, "relatedrecclass": "Child Class4" }, { "recordid": 2, "recordidclass": "Parent Class", "relatedrecid": 5, "relatedrecclass": "Child Class5" }, { "recordid": 5, "recordidclass": "Parent Class", "relatedrecid": 6, "relatedrecclass": "Child Class6" }, { "recordid": 3, "recordidclass": "Parent Class", "relatedrecid": 7, "relatedrecclass": "Child Class7" }, { "recordid": 4, "recordidclass": "Parent Class", "relatedrecid": 8, "relatedrecclass": "Child Class8" }]; var someId = 1; var d1 = data.find(e => e.recordid == someId); var d2 = data.filter(e => e.recordid == d1.relatedrecid); d2.unshift(d1); console.log(d2); 

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