2
const conditionalArray = [
  { name: "line", condition: ">=", value: 5 },
  { name: "revene", condition: "in", value: 6 },
];

const dataTofilter = [
  { line: 3, revene: 4, sale: 3, fridge: "lg" },
  { line: 6, revene: 3, sale: 2, fridge: "samsung" },
];

I have these 2 arrays one is having conditions ( many more can be there) and second is to filter

Final result should be [{ line: 6, revene: 3, sale: 2, fridge: "samsung" }]

6
  • 1
    Familiarize yourself with how to access and process nested objects, arrays or JSON and use the available static and instance methods of Object and Array. Commented Aug 17, 2021 at 18:01
  • @Behemoth Can you please let me know how to filter using conditional array Commented Aug 17, 2021 at 18:06
  • What is { name: "revene", condition: "in", value: 6 } supposed to mean? revene in 6 is invalid syntax. Commented Aug 18, 2021 at 17:31
  • @Sebastian instead of 6 some string revene in (‘abc’) Commented Aug 18, 2021 at 17:33
  • Ah, okay, so in: (a, b) => b.includes(a) or similar as a function property in the approach below, right? The question still says value: 6 instead of value: "abc" and numbers cannot be searched by contents. Commented Aug 18, 2021 at 17:35

3 Answers 3

2

Something along the lines of:

const conditionalArray = [
  { name: "line", condition: ">=", value: 5 },
  { name: "revene", condition: "<", value: 6 },
];

const dataTofilter = [
  { line: 3, revene: 4, sale: 3, fridge: "lg" },
  { line: 6, revene: 3, sale: 2, fridge: "samsung" },
];

const conditions = {
  '<': (x, y) => x < y,
  '<=': (x, y) => x <= y,
  '>': (x, y) => x > y,
  '>=': (x, y) => x >= y,
};

const result = dataTofilter.filter(data => {
  for (const el of conditionalArray) {
    if (!conditions[el.condition](data[el.name], el.value)) {
      return false;
    }
  }
  return true;
});

console.log(result);

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

6 Comments

Wow beautiful one!
@MikeM Yeah I know .Wow best answer .. Thanks a ton !!
@MikeM Can you let me know if condition contains in operator how to make function
@Singla How does in relate to the value?
@MikeM can you help me here var searchQuery = {}; //or something specific like {name: 'Kitten Name',name: ‘cat’; var foundKittens = []; Kitten.find(searchQuery , function (err, kittens) { if (err) foundKittens = null } else { foundKittens = kittens } });
|
0
const newData = dataTofilter.filter((item) => {
   const passedConditions = [];
   conditionalArray.forEach((cond) => {
       switch(cond.condition){
           case ">":
              passedCondition.push(item[cond.name] > cond.value);
              break;
           case ..... //other conditional checks
       }
     }); //end forEach

     return Arrays.asList(passedCondition).contains(false) === false;
});

If you don't want to use eval you can do something like this. We loop through all the data first and use filter to only return values that pass our conditional check.

We then loop trough the conditional array testing each condition on the single item and save the value to passedConditions. You will need to add the rest of the conditions.

Finally we return wether the passedCondtions array did not contain a false value. This would mean we passed all the conditional checks.

Comments

0

I'm really not a big fan of it. But I don't see another way as eval() to make the condition work as an actual operator without doing plenty of manual checks. Also the following solution assumes that every item in dataToFilter has a corresponding item in conditionalArray.

const conditionalArray = [
  { name: "line", condition: ">=", value: 5 },
  { name: "revene", condition: "<", value: 6 },
];

const dataTofilter = [
  { line: 3, revene: 4, sale: 3, fridge: "lg" },
  { line: 6, revene: 3, sale: 2, fridge: "samsung" },
];

const result = dataTofilter.filter((item, i) =>
  eval(
    `${item[conditionalArray[i].name]}${conditionalArray[i].condition}${
      conditionalArray[i].value
    }`
  )
);

console.log(result);

3 Comments

I would advise you to avoid eval. Use const operators = { "<"(a, b){ return a < b; }, ">="(a, b){ return a >= b; } };, etc., then return operators[conditionalArray[i].condition](conditionalArray[i].name, conditionalArray[i].value);. Also, the question sounds like all conditions should be applied to all dataToFilter objects, not one-by-one, index-by-index.
Yes, I know it's no a brilliant idea to use eval. However it indeed makes it quite a lot simpler for that small problem. If it gets more serious I would, of course, also go with the manual case detection.
Go with @MikeMs solution then. It should be the accepted one for this post in my opinion. I misinterpreted your question a little.

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.