gpt4 book ai didi

javascript - 删除数组元素

转载 作者:行者123 更新时间:2023-11-30 10:18:09 24 4
gpt4 key购买 nike

我将从伪代码开始:

array = copper, oil, silver, bronze, gold, iron

user inputs the letter 'l'

if (the input has the letter 'l', remove everything from the array except words with 'l') {

output = copper, bronze, iron

我的代码:

//arr = iron, oil, gold, silver, bronze

classDiv = document.getElementsByClassName("searchOutputName");

for(var i = 0; i < arr.length; i++){

market = arr[i];

var n = market.indexOf(userInput.value);

if (n >= 0) {
classDiv[i].appendChild(document.createTextNode(arr[i]));

}
else {
//do nothing
}

我的搜索功能有 5 个 div 框。

如果用户输入“l”,第一个框是空的,然后是油、金、银,然后是另一个空框。我想把它们叠起来……第一个盒子里有油,然后是金子,然后是银子,然后是 2 个空盒子。

最佳答案

您还可以为 javascript 使用过滤器:

var arr = ["copper", "oil", "silver", "bronze", "gold", "iron"];
arr = arr.filter(function(item){ return item.indexOf('l')>=0;});
console.log(arr);

关于javascript - 删除数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com