Home > Back-end >  JavaScript For Loop skips some items in an array
JavaScript For Loop skips some items in an array

Time:07-08

I'm trying to remove all of the items with more than one 'o' from the itCompanies array and then print the array to console. I'm not sure why, but when I'm splitting each item in the array to letters to check if there are more than one 'o', Google and Apple both get skipped.

const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'];
for (let i = 0; i < itCompanies.length; i  ){
    let s = itCompanies[i].split('');
    let count = 0;
    for (let j = 0; j < s.length; j  ){
        if (s[j] == 'o' ){
            count  ;
        }
    }
    if (count >= 2){
        itCompanies.splice(i, 1);
    }
}
console.log(itCompanies);

Output

Array(5)
0: "Google"
1: "Apple"
2: "IBM"
3: "Oracle"
4: "Amazon"

Wanted Output

Array(5)
0: "Apple"
1: "IBM"
2: "Oracle"
3: "Amazon"

CodePudding user response:

I think it's better to replace the O's and then compare the lengths from the original string vs the string after the replacement.

const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'];
const result = itCompanies.filter(s => s.length - s.replace(/o/g, "").length < 2);

console.log(result);

The splice has an effect on the indexes because you're mutating the array, you can work around this by looping in a backward direction.

const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'];
for (let i = itCompanies.length - 1; i >= 0; i--){
    let s = itCompanies[i].split('');
    let count = 0;
    for (let j = 0; j < s.length; j  ){
        if (s[j] == 'o' ){
            count  ;
        }
    }
    if (count >= 2){
        itCompanies.splice(i, 1);
    }
}
console.log(itCompanies);

CodePudding user response:

Splice is basically removing the items dynamically, inside the for loop so the index of the items in the array also changes.

CodePudding user response:

Instead of splicing the array, which changes the index , you could move the item, you like to keep into the place of deleted items and change the length at the end.

This approach keeps the same object reference to the array, as in the question.

const
    itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'];

let i = 0, j = 0;
    
while (i < itCompanies.length) {
    if (!(itCompanies[i].match(/o/g)?.length > 1)) {
        itCompanies[j] = itCompanies[i];
        j  ;
    }
    i  ;
}

itCompanies.length = j;

console.log(itCompanies);

CodePudding user response:

You can achieve this by using Array.filter() method along with Array.some().

Live Demo :

const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'];

const res = itCompanies.filter(company => {
    return ![...company].filter((c1, i1, a) => a.some((c2, i2) => {
    if (c1 === 'o') {
        return c1 == c2 && i1 != i2
    }
  })).join('');
});

console.log(res);

  • Related