0

Input JSON

0===>{"eid":12,"gender":"1","age":1,"pass":["2","1"]}
1===>{"eid": 11,"gender":"0","age":1,"pass":["1","3"]}
2===>{"eid":20,"gender":"1","age":1,"pass":["2","3"]}

how to create new array.. to push the ids based on the pass numbers

Ex: IN a loop display

passid => 2 .... eid => 12, 20

2 ==> ["12","20"]
1 ==> [12, 11]
3 ==> [11,20]
2
  • 1
    Makes no sense at all, and doesn't even look like valid JSON ? Commented Aug 19, 2015 at 16:53
  • please refer the ans from andy .. Commented Aug 19, 2015 at 17:27

1 Answer 1

2

Use filter and some to check the contents of the pass array and then return the respective eid values:

function grabber(data, pass) {
    return data.filter(function (el) {
        return el.pass.some(function (num) {
            return +num === pass;
        })
    }).map(function (el) {
        return el.eid;
    });
}

grabber(data, 1); // [12, 11]
grabber(data, 2); // [12, 20]
grabber(data, 3); // [11, 20]

DEMO

UPDATE

Realised on the way home from work you don't actually need some. Further, to answer your comment, here's how you might search for pass and gender:

function grabber(data, options) {
    return data.filter(function (el) {
        return el.pass.indexOf(options.pass) > -1 && el.gender === options.gender;
    }).map(function (el) {
        return el.eid;
    });
}

grabber(data, { gender: '0', pass: '1' }); // [11]

DEMO

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

1 Comment

Thank you, .. here possible to filter with gender and pass id ?? grabber(data, 0, 1); // [ 11]

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.