2

I am working with jquery and ajax.I want to select json object having same element. ie, here is my json object.

 {"data":[{"row":15,"name":"aaa"},
          {"row":15,"name":"bbb"},
          {"row":10,"name":"ffff"},
          {"row":15,"name":"rrr"}
 ]}

Then, I want to select all 'name' having same 'row'. How shall I do it using jquery?

1
  • Could you clarify this? What do you mean by selecting all that have the same row? How do you decide which row to get? Will there only ever be 2 different rows? Commented Mar 20, 2015 at 12:21

2 Answers 2

2

Array.filter() if you want a pure js solution...

success: function(result) {
    var test = 15;
    var subset = result.data.filter(function(o) { return o.row === test; });
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use grep for that.

var obj = {
    "data": [{
        "row": 15,
        "name": "aaa"
    }, {
        "row": 15,
        "name": "bbb"
    }, {
        "row": 10,
        "name": "ffff"
    }, {
        "row": 15,
        "name": "rrr"
    }]
};

function filterData(rowNumber) {
    var filteredrObject = jQuery.grep(obj.data, function(element, i) {
        return element.row == rowNumber;
    });
    return filteredrObject
}
console.log(filterData(15));

Fiddle

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.