循环的JavaScript跳过数组中的一些项目

发布于 2025-02-13 22:16:50 字数 730 浏览 3 评论 0 原文

我正在尝试从ITCompanies数组中删除所有具有多个“ O”的项目,然后将数组打印为控制台。 我不确定为什么,但是当我将阵列中的每个项目分开到字母以检查是否有一个以上的“ O”时,Google和Apple都会被跳过。

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);

输出

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

想要输出

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

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街角迷惘 2025-02-20 22:16:52

您可以通过使用 方法与 aray> array.some.some.some.some.some()

实时演示

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);

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);

梦巷 2025-02-20 22:16:51

剪接基本上是在for循环内部动态删除项目,因此数组中项目的索引也会改变。

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

聚集的泪 2025-02-20 22:16:51

您可以移动项目,而是要插入索引的数组,而是要移入删除项目的位置并在末尾更改长度。

该方法与问题相同的对象引用数组。

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);

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);

长亭外,古道边 2025-02-20 22:16:50

我认为最好替换O,然后比较替换后原始字符串与字符串的长度。

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);

剪接会对索引产生影响,因为您正在突变数组,因此可以通过向后循环来解决此问题。

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);

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文