1

This is my JSON response . I am able to splice out elements from controller side using something like this . Now comes a situation where my employees can have same employee id but different Type . So i would like to remove itesm from the response by comparing both Id & type. For example I need to remove the emplooyee with Id ABC and type D only . How can i splice them out how can i proceed

var searchresponse = [{
    "items": [{
        "employeeId": "ABC",
        "type": "D",
        "alive": "Yes"

    }, {
        "employeeId": "ABC",
        "type": "P",
        "alive": "Yes"

    }, {
        "employeeId": "NPK",
        "type": "D",
        "alive": "Yes"

    }, {
        "employeeId": "PKN",
        "type": "A",
        "alive": "Yes"
    }],
    "more": false
}];

var data1 = ["ABC"];
var data2 = ["D"] //- how to splice ABC also comparing the D 
var items = searchresponse[0].items;
for (var i = items.length - 1; i >= 0; i--) {
    if (data1.indexOf(items[i].employeeId) != -1) {
        items.splice(i, 1);
    }
}
3
  • "items": [{ "employeeId": "ABC", "type": "D", "alive": "Yes" }] do you need a reponse like this ? Commented Dec 23, 2016 at 15:29
  • I have an Response like that .. I need to splice out that particular id ABC from the Search response Commented Dec 23, 2016 at 15:32
  • Can you simply iterate over the response? stackoverflow.com/questions/31868918/… Do a foreach and check the values Commented Dec 23, 2016 at 15:43

2 Answers 2

1

If i understand u right.. will it be ok to just extend the if clause?

if (data1.indexOf(items[i].employeeId) != -1 && data2.indexOf(items[i].type) != -1)

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

1 Comment

I should have tried this before posting it :) it worked !!
1

Using array.prototype.filter method

 var filterd =searchresponse[0].items.filter(function (item) {
 return item.employeeId == "ABC"&& item.type== "D"
})

You may Also use underscore.js simple javascript library in your case

var filterd=_.where(searchresponse[0].items, {employeeId: "ABC", type: "D"});

will give you

 [{
    "employeeId": "ABC",
    "type": "D",
    "alive": "Yes"

}]

Check this working 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.