最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Remove all elements from array of strings that do not contain "IN" - Stack Overflow

matteradmin3PV0评论

I need to remove all elements in an array that do not contain "IN" in uppercase exactly like that.

How I thought of doing this was to traverse the array with a for loop and write all values that contain IN to another array.

Is there a way I can do it without writing to a new array and just removing those items that don't match from the current array?

Here is the code for how I was planning on doing it:

arrTwo = [];

for(var i = 0; i<arr.length; i++){
    if(arr[i].indexOf('IN') > -1) arrTwo.push[arr[i]];
}

I need to remove all elements in an array that do not contain "IN" in uppercase exactly like that.

How I thought of doing this was to traverse the array with a for loop and write all values that contain IN to another array.

Is there a way I can do it without writing to a new array and just removing those items that don't match from the current array?

Here is the code for how I was planning on doing it:

arrTwo = [];

for(var i = 0; i<arr.length; i++){
    if(arr[i].indexOf('IN') > -1) arrTwo.push[arr[i]];
}
Share asked Jun 13, 2014 at 22:32 MrGuruMrGuru 3451 gold badge6 silver badges16 bronze badges 1
  • There is filter function defined for the array object which you can use. It will create a new filtered array though – Andrei Commented Jun 13, 2014 at 22:39
Add a ment  | 

3 Answers 3

Reset to default 13

You can use ES5 filter method:

arr = arr.filter(function(s){
    return ~s.indexOf("IN");
});

And using ES6 arrow functions, it can be simplified to:

arr = arr.filter(s=>~s.indexOf("IN"));

Here's a really good thread that has a couple of ways to acplish this. If you do not delete the element of the array in the correct manner, you that element will be undefined rather than actually deleted. The .spilce() method is what you want to look into.

Deleting array elements in JavaScript - delete vs splice

I would do it using the splice() method:

var testArray = [ 'this one contains IN', 'this one does not' ];

function filterArray ( arr ) {
    var i = arr.length;
    //-- Loop through the array in reverse order since we are modifying the array.
    while (i--) {
        if (arr[i].indexOf('IN') < 0) {
            //-- splice will remove the non-matching element
            arr.splice(i, 1);
        }
    }
}

filterArray( testArray );

document.body.innerText = JSON.stringify(testArray);

JSFiddle: http://jsfiddle/5DW8L/1/

Post a comment

comment list (0)

  1. No comments so far