0
var techologylist=[1,2,2,3,4,4,5] var newtechologylist=[]; $.each(techologylist, function (i, el) { if ($.inArray(el, newtechologylist) === -1) newtechologylist.push(el); }); console.log(newtechologylist) 

I want to remove values from techologylist ,if same 2 value comes .In my example 2 and 4 comes twice ,therefore i want to remove both 2 and 4 from array Am expecting result as [1,3,5]

But result for my code of course give result as [1,2,3,4,5]

How to change my script to remove both values if duplicate occurs

5
  • I believe I just removed a duplicate from the questions array... Commented Oct 26, 2015 at 7:46
  • 1
    I think it is not duplicate.Both cases are different Commented Oct 26, 2015 at 7:49
  • And to attempt to answer the question, I'd have two Objects, one of which keeps track of all values seen, and the other which will contain the output. Loop through the input, if the current value is in the first Object already, delete it from the output Object. If it's not in the first Object, add it to both. Commented Oct 26, 2015 at 7:55
  • Right, misleading title much... Reopened. Commented Oct 26, 2015 at 7:56
  • var techologylist=[1,2,2,3,4,4,5]; var newtechologylist=[]; $.each(techologylist, function (i, el) { if ($.inArray(el, newtechologylist) === -1) { newtechologylist.push(el); } else { var index = newtechologylist.indexOf(el); if (index > -1) { newtechologylist.splice(index, 1); } } }); console.log(newtechologylist) Commented Oct 26, 2015 at 8:05

1 Answer 1

1

var techologylist = [1, 2, 2, 3, 4, 4, 5] var newtechologylist = []; $.each(techologylist , function (i, el) { if ($.inArray(el, newtechologylist ) === -1) { newtechologylist.push(el); } else { var index = newtechologylist.indexOf(el); if(index > -1){ newtechologylist.splice(index, 1); } } }); document.write(newtechologylist);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

1 Comment

thanks ozil ,exactly what i want

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.