1

There is a way to filter (or query) an array structured like this:

[   {
            'xml:id': 'Name1',
            sex: {
                '$t': 'M'
            },
            occupation: {
                n: 1,
                '$t': 'bank'
            }
        },
        {
            'xml:id': 'Name2',
            sex: {
                '$t': 'M'
            },
            occupation: {
                n: 1,
                '$t': 'writer'
            }
        }, {
            'xml:id': 'Name3',
            sex: {
                '$t': 'F'
            },
            occupation: {
                n: 1,
                '$t': 'bank'
            }
        }, {
            'xml:id': 'Name4',
            sex: {
                '$t': 'M'
            },
            occupation: {
                n: 1,
                '$t': 'writer'
            },
        }, {
            'xml:id': 'Name5',
            sex: {
                '$t': 'M'
            },
            occupation: {
                n: 1,
                '$t': 'writer'
            }
        }
    ]

whit a query of this type:

const pers_query: Query = {
  type: 'person',
  args: [
    {
      key: 'occupation',
      value: 'bank',
    },
    {
      key: 'sex',
      value: 'f',
    },
  ],
};

What I want is to filter the data with multiple keys and values, in this specific case I want to filter the data for all the occurrences of object that contains the key sex with value f AND the key occupation with value bank

the result should be something like this:

[
   {
        'xml:id': 'Name3',
        sex: {
            '$t': 'F'
        },
        occupation: {
            n: 1,
            '$t': 'bank'
        }
    }
]
4
  • It's invalid. Use jsonlint.com to check. If the exact format doesn't matter, then you probably don't mean JSON, but instead a JS Object. Commented Jul 14, 2021 at 9:11
  • 1
    There is no JSON here, it's just an array of objects. Commented Jul 14, 2021 at 9:16
  • yes, I'm so sorry I wrote JSON data because it's an extraction of an array of objects inside a JSON structure. My bad. Commented Jul 14, 2021 at 9:24
  • 1
    @RootAtKali no problem. It's a common mixup Commented Jul 14, 2021 at 9:28

2 Answers 2

2

You can use a combination of Array.prototype.filter() and Array.prototype.each

const input = [{    'xml:id': 'Name1',    sex: {      '$t': 'M'    },    occupation: {      n: 1,      '$t': 'bank'    }  },  {    'xml:id': 'Name2',    sex: {      '$t': 'M'    },    occupation: {      n: 1,      '$t': 'writer'    }  }, {    'xml:id': 'Name3',    sex: {      '$t': 'F'    },    occupation: {      n: 1,      '$t': 'bank'    }  }, {    'xml:id': 'Name4',    sex: {      '$t': 'M'    },    occupation: {      n: 1,      '$t': 'writer'    },  }, {    'xml:id': 'Name5',    sex: {      '$t': 'M'    },    occupation: {      n: 1,      '$t': 'writer'    }  }]
const pers_query = {
  type: 'person',
  args: [{
      key: 'occupation',
      value: 'bank',
    },
    {
      key: 'sex',
      value: 'f',
    },
  ],
};

const output = input.filter(person => {
  return pers_query.args.every(condition => {
    return person[condition.key]['$t'].toLowerCase() === condition.value.toLowerCase();
  });
});

console.log(output);

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

2 Comments

Thank you so much, this is what I looking for.
fee.l free to mark it as accepted and/or upvote as you see fit
0
function filterData(data,query,caseSensitive=false) {
    let filteredData = data;
    // no use for type since it's  not in data
    query.args.forEach(arg => filteredData = filteredData.filter(item => caseSensitive ? item[arg.key]['$t'] === arg.value : item[arg.key]['$t'].toLowerCase() === arg.value.toLowerCase())); // you can use .lowe

    return filteredData;
}

This is one way to do it, note that we assign filteredData with data at beginning since where applying filter and that won't cause any change to the original array.

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.