0

I have an array with color names:

var colors = ["yellow","yellow","yellow","orange","orange","blue","blue","blue","blue];

And an array with indexes:

var indexes = [2, 3];

I would like to remove color names on the positions given by indexes – in this example the third "yellow" and the first "orange". I tried a loop like this:

for (var i = colors.length - 1; i >= 0; i--) {
    for (var j = 0; j < indexes.length; j++) {
        if (colors.indexOf(colors[i]) == indexes[l]) {
            colors.splice(k, 1);
        }
    }
}

The trouble is that colors.indexOf(colors[i]) gives the same value for all repeating color names. Is there a better way?

3
  • So 2 is yellow and 3 is orange, so you want to remove all the yellows and oranges? Commented Dec 22, 2017 at 20:31
  • Why are you looking for the index of? You have an array of indexes so I'm really confused? Commented Dec 22, 2017 at 20:34
  • I am trying to build a Tetris game. I have an array with numbers of squares. I am trying to delete squares in a full row. Then I want to delete colors on same indexes in a separate array with colors. The indexes are from another function, which is looking for full rows.. Commented Dec 22, 2017 at 20:57

5 Answers 5

2

You can use filter and includes functions

colors = colors.filter((e, i) => !indexes.includes(i));
Sign up to request clarification or add additional context in comments.

Comments

0
for (var i = indexes.length-1; i >= 0; i--)
   colors.splice(indexes[i], 1);

Provided of course indexes contains increasing element indexes. If not, sort it first.

Edit: Having read a comment I realise perhaps you meant you wanted to remove all colours with the names at position 2,3. To be clear this just removes the elements at position 2,3 and only those elements.

1 Comment

Thanks. I want just to delete elements at positions 2 and 3. I am trying to build a Tetris game. I have an array with numbers of squares. I am trying to delete squares in a full row. Then I want to delete colors on same indexes in a separate array with colors.
0

You could loop from the end and splice the item. This reduces the length of the array.

You can not loop from the beginning, because after splicing the next index is the actual index.

var colors = ["yellow0", "yellow1", "yellow2", "orange0", "orange1", "blue0", "blue1", "blue2", "blue3"],
    indexes = [2, 3],
    i = indexes.length;
    
while (i--) {
    colors.splice(indexes[i], 1);
}

console.log(colors);

Comments

0

My suggestion would be to first loop through the indexes array rather than the colors. That way you can obtain the color you want to remove first. Then you can use whatever method you think is best to remove all instances of that color. In this example I used Array#Filter and a function that checks if the color in the array matches the color obtained form the index.

A for loop could have also been used.

var colors =["yellow","yellow","yellow","orange","orange","blue","blue","blue","blue"]


var indexes = [2, 3];

for(let i = 0; i < indexes.length; i++){
  let color = colors[indexes[i]];
  colors = colors.filter( c=>c!=color )
}

console.log(colors)

Comments

-1

Try creating a new array:

var colors = ["yellow","yellow","yellow","orange","orange","blue","blue","blue","blue"];
var newcolors=[];
colors.forEach(function(item) {
    if(newcolors.indexOf(item)==-1)
    {
        newcolors.push(item);
    }
})
colors = newcolors;

1 Comment

You can also run through the array itself, and splice elements that repeat in the array. There are very likely more efficient ways to do this, but here's just one

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.