javascript - Find and remove object in array by value in nested array -
i have array following structure:
var questions = [ {"answer": ["a1", "a2", "a3"] }, {"answer": ["b1"] }, {"answer": ["c1", "c2"] } ]
i have array of objects below:
var answers = [ {"value": "a1"}, {"value": "a512"}, {"value": ""}, {} ];
for each object in answers
(if it's not {}
) find if corresponding value appears in object inquestions
. if does, update counter
variable , remove object questions
. how achieve this?
try
var questions = [{ "answer": ["a1", "a2", "a3"] }, { "answer": ["b1"] }, { "answer": ["c1", "c2"] }]; var count = 0; answers.foreach(function(ans) { var flag = isexists(ans.value); flag && count++; }); console.log(count); function isexists(value) { return questions.some(function(ques) { var ind = ques.answer.indexof(value); if (ind > -1) { ques.answer.splice(ind, 1); return true; } }); }
Comments
Post a Comment