2

I have a JavaScript object that I use to store data for one user that can look like this:

output = {
  id: "444",
  trial: [1, 2, 3, 4, 5, 6, 7, 8, 9],
  points: [0, 100, 50, 50, 0, 0, 0, 100, 50]
  }

What I want is to query/filter this object, for example, to extract all trial numbers output.trial where output.points > 50.

I have found this in another post but it is not quite what I am looking for (it returns an empty array).

var result = $.grep(output, function(v) {
    return v.points > 50;
});

In other words, I want to give a number of conditions and receive the instances of a name of my object where this is true (preferable as array). In this example:

result_after_query = [2, 8]

How can I achieve this?

2 Answers 2

5

You can use Array.prototype.filter method:

var output = {
  id: "444",
  trial: [1, 2, 3, 4, 5, 6, 7, 8, 9],
  points: [0, 100, 50, 50, 0, 0, 0, 100, 50]
};

var result = output.trial.filter(function(el, i) {
  return output.points[i] > 50;
});

document.write(JSON.stringify(result));

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

Comments

1

use below code, it will surely help u

var output = {id: "444",trial: [1, 2, 3, 4, 5, 6, 7, 8, 9],points: [0, 100, 50, 50, 0, 0, 0, 100, 50]};

var arr = [];

$.each(output.points, function(index, value){
    if(value > 50)
      arr.push(index +1);
});
alert(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

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.