0

I'm writing a JS code that has to filter JSON data based on a condition. I've posted a question yesterday but without trying anything at How to filter data from a json response, it was down voted and since I didn't try anything I am good with it. I've tried using the filter, but unfortunately I've got an array with in array and here I'm stuck. Here is my code.

var arr2 = [
  [{
    "max": "0.685",
    "target_state": "6",
    "ingredients": "a",
    "rule_no": "7",
    "id": "18"
  }, {
    "max": "14",
    "target_state": "6",
    "ingredients": "b",
    "rule_no": "7",
    "id": "17"
  }],
  [{
    "max": "14",
    "target_state": "7",
    "ingredients": "b",
    "rule_no": "8",
    "id": "19"
  }, {
    "max": "1.36",
    "target_state": "7",
    "ingredients": "a",
    "rule_no": "8",
    "id": "20"
  }]
];

var result = arr2.reduce(function(a, b) {
    return a.concat(b);
  })
  .filter(function(obj) {
    return (obj.max == 0.685 && obj.ingredients === "a")
  });

alert(JSON.stringify(result[0].target_state));

when I run this code, I get the result as "6".

But here my condition is something like this ((obj.max == 0.685 && obj.ingredients === "a") && (obj.max == 14 && obj.ingredients === "b")), but this is not returning anything.

Here the expected output is "6".

Here is a working fiddle https://jsfiddle.net/vjt45xv4/14/

Please let me know where am I going wrong and how can I fix this.

Thanks

11
  • 1
    Can you also share expected output based on max and ingredients value? Commented Jan 17, 2018 at 6:56
  • It should be (...) || (...). If obj.max is 0.685, it cannot be 14. Always try to write an English version of condition. It will always help in solving them. ((obj.max == 0.685 && obj.ingredients === "a") && (obj.max == 14 && obj.ingredients === "b")) is interpreted as max value should be 0.685 and ingredients should be a and max value should be 14 and ingredients should be b Commented Jan 17, 2018 at 6:58
  • Are you trying to select the first, second, and third elements, or just the first and second? Also, I would suggest not using == in order to compare a number to a string. If you want to compare a string value to a number, parse the string: Number(obj.max) === 0.685 Commented Jan 17, 2018 at 7:07
  • @gurvinder372, updated my question with expected result Commented Jan 17, 2018 at 7:07
  • @JLRishe, Here I've an array like this [[{}, {}],[{}, {}]] and here I want to check the internal arrays matching the condition(both the internal arrays should satisfy it) and return the result. Commented Jan 17, 2018 at 7:09

1 Answer 1

1

I think I understand what you are trying to achieve here.

How about the following one? It worked for me.

var result = arr2
  .filter(function(objs) {
    return ((objs.find((obj) => obj.ingredients === "a" ).max == 0.685) 
&& (objs.find((obj) => obj.ingredients === "b" ).max == 14));
  })
  .reduce(function(a, b) {
    return a.concat(b);
  });
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for this friend. But I'm matching for both, not either
@user3872094 You want objects matching both the conditions within sub array?
Yes Nabin, you are perfect... That's my requirement
@user3872094 I now understand what you are trying to achieve. I have provided the solution. Please check.
You are awesome Bro ... This was what I was expecting :)
|

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.