【问题标题】:Filter an Array of strings using Filtered String and Remove from Original Array使用过滤字符串过滤字符串数组并从原始数组中删除
【发布时间】:2022-01-16 08:00:36
【问题描述】:

我想从包含单词 Evil (filterString) 的数组中删除一些元素。

let guests = ["Partner", "Evil Nice Relative 1", "Nice Relative 2", "Evil One", "another evil", "another one", "another evil is here", "strange Evil is here", "someone Nicer", "Ugly Evil Bad"];

const filteredArray = [];
const filterString = "Evil";

function checkEvil() {
    guests.filter((element, index) => {
        if (element.toLowerCase().indexOf(filterString.toLowerCase()) !== -1) {
            console.log(index);
            guests.splice(index,1);
        } else {
            filteredArray.push(element);
        }
    });
    console.log(guests);
}

这是我得到的原始数组(guests):

['合作伙伴','好亲戚2','另一个邪恶','另一个','奇怪的邪恶在这里','更好的人']

只希望在过滤所需字符串 (Evil) 后更新 guests 数组。

【问题讨论】:

  • 查看filter 和includes 或indexOf 方法,developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 不知何故原始代码没有出现在这里。编辑了问题。
  • 看看filter方法是如何工作的,它返回新的元素数组,当回调的输出为真时返回元素,所以你不需要使用额外的数组。

标签: javascript arrays filtering array-splice


【解决方案1】:

既然你想改变原始数组,那么你可以这样做:

let guests = [
  "Partner",
  "Evil Nice Relative 1",
  "Nice Relative 2",
  "Evil One",
  "another evil",
  "another one",
  "another evil is here",
  "strange Evil is here",
  "someone Nicer",
  "Ugly Evil Bad",
];

const filterString = "Evil";

function checkEvil() {
  for (let i = guests.length - 1; i >= 0; i--) {
    const element = guests[i];
    if (element.toLowerCase().indexOf(filterString.toLowerCase()) !== -1) {
      guests.splice(i, 1);
    }
  }
  console.log(guests);
}

checkEvil();

1) 您可以使用filter 和match 轻松实现结果:

const arr = [
  "Partner",
  "Nice Relative 2",
  "another evil",
  "another one",
  "strange Evil is here",
  "someone Nicer",
];
const result = arr.filter((s) => !s.match(/evil/i));
console.log(result);

2) 你也可以使用 forEach 和 match 来做到这一点:

let guests = [
  "Partner",
  "Evil Nice Relative 1",
  "Nice Relative 2",
  "Evil One",
  "another evil",
  "another one",
  "another evil is here",
  "strange Evil is here",
  "someone Nicer",
  "Ugly Evil Bad",
];

const filteredArray = [];
const filterString = "Evil";

function checkEvil() {
  guests.forEach(element => {
    if (!element.match(/evil/i)) filteredArray.push(element);
  });
}
checkEvil();
console.log(filteredArray);

【讨论】:

  • 不知何故原始代码没有出现在这里。编辑了问题。
  • @Tahir 没什么变化,所以你仍然可以在这里使用过滤和匹配
  • @Tahir 添加了另一个答案。看看
  • 但我确实想更新原始数组 guests。
  • 如果你想更新原始数组那你为什么要使用filteredArray
【解决方案2】:

定义一个模式,然后按它过滤

var arr = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
const PATTERN = 'EVIL';
arr = arr.filter(str => str.toUpperCase().indexOf(PATTERN) === -1);

【讨论】:

    【解决方案3】:
    <script>
        var list = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
        var new_list = [];
    
        for(var i=0; i<list.length; i++) {
            if(!list[i].toLowerCase().includes('evil')) {
                new_list.push(list[i]);
            }
        }
    
        console.log(new_list);
    </script>
    

    【讨论】:

    • 让我们说初学者技术)
    【解决方案4】:

    您可以过滤每个值不包含 'evil' 单词的数组:

    const arr = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
    const filteredArr = arr.filter(val=>!val.toLowerCase().includes('evil'))
    console.log(filteredArr)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-06
      • 2017-07-15
      • 2019-04-04
      • 1970-01-01
      • 2022-07-04
      • 2014-12-07
      • 2021-07-23
      相关资源
      最近更新 更多