简体   繁体   English

如何使用for循环和拼接来删除单词,然后检查数组中的特定单词

[英]How to use a for loop and splice to remove a word and then check an array for a specific word

I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. 我想制作一个在数组中查找特定名称(Inger)的函数,然后删除该名称。 Then I want the function to tell that a name doesn't exist in the array. 然后,我要函数告诉该名称在数组中不存在。

    var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]

    function removeElement (aTable, aName) {

        for (var i = 0; i <= aTable.length - 1; i++) {
            if (aTable[1] === aName) {

                aTable.splice(i, 1)
                document.write(aTable); {break;}

            } else if (aTable[i] !== aName) {
                document.write(aName + " is not in the list");
            }
        }
    }

I've tried to solve it this way, but I don't get it right. 我试图用这种方式解决它,但是我做得不好。 The output should be something like this: 输出应该是这样的:

Anne, Kari, Marit, Ingrid
Victoria is not in the list

Do you have to write functions? 您必须编写函数吗? Javascript has Array methods to do this for you. JavaScript具有Array方法可以为您完成此任务。

Array.prototype.filter() Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter()方法创建一个新数组,其中所有元素都通过了由提供的函数实现的测试。

includes() include()

The includes() method determines whether an array includes a certain element, returning true or false as appropriate. includes()方法确定数组是否包含某个元素,并在适当时返回true或false。

 var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"] femaleName = femaleName.filter(name => name !== 'Inger') console.log(femaleName); console.log(femaleName.includes('Inger')); 

The issue is this line: 问题是此行:

if (aTable[1] === aName) {

That only checks the second index ( "Inger" ). 那只会检查第二个索引( "Inger" )。 It should be this: 应该是这样的:

if (aTable[i] === aName) {

You could do this: 您可以这样做:

function removeElement(aTable, aName) {
    const index = aTable.indexOf(aName);
    if (index > -1) {
        aTable.splice(index, 1);
        document.write(aTable);
    } else {
        document.write(aName + " is not in the list");
    }
}

Or you could always use functional Array.prototype.filter(). 或者,您始终可以使用功能性Array.prototype.filter()。

Following your description, try this example. 按照您的描述,尝试以下示例。 That first gets the index in the array of the searched name using findIndex , then removes it from the array using splice and finally prints the result 首先使用findIndex获取搜索名称数组中的索引,然后使用splice将其从数组中删除,最后打印结果

 const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid']; function notify(criteria) { const position = getPosition(criteria); const removed = removeName(position); console.log(names); console.log(`${removed} is not in the list`); } function getPosition(criteria) { return names.findIndex(name => name === criteria); } function removeName(position) { return names.splice(position, 1); } notify('Inger'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM