2

So I have an array with 7 values:

var names = ["Tom", "James", "Matthew", "Jordy", "Jeremy", "Jasper", "Jordy"]

I need to get rid of both "Jordy", not just one, but both of them.

I'm using plain JS and a bit of jQuery. I know I can filter out one of them, but I don't know how to do both.

the result would have to be:

var names = ["Tom", "James", "Matthew", "Jeremy", "Jasper"]
9
  • What specific code have you tried and how old an IE version must you support? You really should show us what you tried and what specific problems you ran into rather than just asking people to write code for you from scratch. There are many, many ways to do this. None are hard. If you can remove one, then you can easily just run the same routine on the rest of the array to remove the rest. Commented Feb 19, 2015 at 23:44
  • I have tried with for loops, but was never able to remove the second one. I always had 6 elements left, not 5 and the version of IE doesn't matter, because it's a crossplatform app with Cordova written in JS ;) I just need a way to do this, because I cannot find it at all. Commented Feb 19, 2015 at 23:47
  • If you are removing an element each time you find a match, then run your for loop from the end of the array to the front so when you remove an element, it won't affect the indexing for the elements you haven't yet seen. And, please paste in the code you tried (into your question using the edit link) and describe what problem you ran into. That's how StackOverflow works best. You may also find the array method .filter() to be easiest if you don't mind the result being in a new array. Commented Feb 19, 2015 at 23:49
  • it's okay :) i managed to find it, with a little tip from my friend. Commented Feb 20, 2015 at 0:04
  • 2
    Why do you want to get rid of me :(? Commented Apr 10, 2018 at 11:35

2 Answers 2

1

If you are OK with the results being returned in a new array, then .filter() is pretty easy:

var names = ["Tom", "James", "Matthew", "Jordy", "Jeremy", "Jasper", "Jordy"];
names = names.filter(function(item) {
    return item !== "Jordy";
});

If you want to modify the current array, you can use .splice() with a backwards traverse.

var names = ["Tom", "James", "Matthew", "Jordy", "Jeremy", "Jasper", "Jordy"];
for (var i = names.length - 1; i >= 0; i--) {
    if (names[i] === "Jordy") {
        names.splice(i, 1);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'll test it out tomorrow ASAP! Thanks for the example code!
1

What you need to do here is avail of both indexOf and lastIndexOf. The other solution will only work if you already know the name of the duplicates that you wish to remove.

names = names.filter(function(item) {
    return names.indexOf(item) === names.lastIndexOf(item);
});

Here we check to see if the position of the first occurrence of the item in the array is equal to the position of the last occurrence of the item in the array. If so, we know the item is not a duplicate. If this predicate fails we know that the item appears more than once in the array and we don't return it.

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.