1

I have an response as a json array with lots of records but i want to filter that json array by using another json array.

my json response

http://prntscr.com/lvxwob

and my filter json array be like

"filterParams" : [ 
        {
            "param" : "actualSum",
            "value" : "95",
            "type" : "text",
            "comparision" : "isEqual"
        }, 
        {
            "param" : "wbsSort",
            "value" : "6",
            "type" : "text",
            "comparision" : "isEqual"
        }
    ],

so how can i filter my response using javascript or node js anything. i want to get filtered data like match param with reponse param and its value.

e.g. if there is match value of actualSum with 95 and wbsSort's value equal to 6 then it will return true other wise false.

2
  • Do you want to return whether there is some results (true or false) as you said or really filtering down the results to the specified parameters ? Commented Dec 17, 2018 at 9:38
  • 3
    please add your try as well. Commented Dec 17, 2018 at 9:39

2 Answers 2

2

You could filter the items in the result array where the item matches every parameter in filterParams. If you only want to check if at least one match exists replace .filter with .some

e.g.

var matches = results.filter(item =>
  filterParams.every(paramItem =>
    item[paramItem.param] === paramItem.value));

I've limited it to equals comparison but you can expand the comparison using a switch based on the other comparison types you have.

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

4 Comments

thank you, but when i log it on my console it will return true as per my filter but final data result array length is 0 why it has happening?
Do you mean the matches array? Could it be that it is being modified elsewhere?
yes i am asking about matches array. no it is not modified elsewhere
It's hard to say what is happening without seeing more code. Might be suited for a new question?
0

A way to do this without having to hardcode every check would be to create a compare function using the filterParams. So you would do something like this, where compareFunction creates a new function using filterParams to initialize the variables to check and returns whether the current item has these values. So for any further check you want to do, you will only have to add it to filterParams. Hope this helps.

const filterParams = [{
        "param" : "actualSum",
        "value" : "95",
        "type" : "text",
        "comparison" : "isEqual"
    }, {
        "param" : "wbsSort",
        "value" : "6",
        "type" : "text",
        "comparison" : "isEqual"
    }];

const data = [{ actualSum: 95, wbsSort: 6 }, { actualSum: 95, wbsSort: 10 }];

const operators = {
    'isEqual': '==='
};

const compareFunction = (params) =>
    (item) => new Function(
        params.reduce((acc, { param, value }) => `${acc} const ${param} = ${value};`, '') +
        params.reduce((acc, { param, value, comparison }, i) => `${acc} ${param} ${operators[comparison]} ${item[param]} ${i !== params.length - 1 ? '&&' : ';'}`, 'return ')
    )();

const filteredData = data.filter(compareFunction(filterParams));

console.log(filteredData);

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.