1

Tell me please how to delete the data entered by the user from the array. For example, I enter (add Tom, add John, add Robert, etc.). The names with indices are added to the array. Now if I enter delete John, I need to delete this particular element and not the first or not the last one. As I understand it, you need to make a loop through the elements of the arrays. but how to do it I can’t figure out.

let arr = [];

for (let i = 0; i < Infinity; i++) {
    var command = prompt('Введите команду');
    var arrays = command.split(' ');

    var index = arrays[0];
    var name = arrays[1];


    if (index == 'add') {
        arr.push(name);
        var res = arr.join(' ');
        console.log(res);
    } else if (index === 'delete' || index === 'del') {

        arr.splice(name, 1);
        console.log(arr);
    }
    if (index === 'stop') {
        break;
    }
    
}
console.log(arr);
  • Does this answer your question? [How can I remove a specific item from an array?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array) – kmoser Sep 09 '21 at 04:00
  • I guess not. As the user is having problem while slicing off the value from the array. OP is using the value instead of the index. Check my answer, you will understand it – BladeOfLightX Sep 09 '21 at 04:03

1 Answers1

1

The mistake was in your delete portion.

In delete, you were using the number instead of the index of the number.

Here is the corrected code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let arr = [];

        for (let i = 0; i < Infinity; i++) {
            var command = prompt("Введите команду");
            var arrays = command.split(" ");

            var index = arrays[0];
            var name = arrays[1];

            if (index == "add") {
                arr.push(name);

                var res = arr.join(" ");
                
                console.log(res);
            } else if (index === "delete" || index === "del") {
                console.log(name)

                const index = arr.indexOf(name)

                if(index !== -1){
                    arr.splice(index, 1);
                } else {
                    console.log("Value not present in list")
                }
                
                
                console.log(arr);
            }
            if (index === "stop") {
                break;
            }
        }
        console.log(arr);
    </script>
</body>
</html>

Now, if you want to remove all the instances from the list, it would be better to use sets.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <script>
        let arr = new Set();

        for (let i = 0; i < Infinity; i++) {
            var command = prompt("Введите команду");
            var arrays = command.split(" ");

            var index = arrays[0];
            var name = arrays[1];

            if (index == "add") {
                arr.add(name);

            } else if (index === "delete" || index === "del") {
                
                const avail = arr.delete(name)

                if(!avail){
                    console.log("Value not in list")
                }

            }
            if (index === "stop") {
                break;
            }
        }
        console.log(arr);
    </script>
</body>

</html>

Lastly, If you want to use arrays to store similar values but remove all of them when user gives del. Use the following code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <script>
        let arr = [];

        for (let i = 0; i < Infinity; i++) {
            var command = prompt("Введите команду");
            var arrays = command.split(" ");

            var index = arrays[0];
            var name = arrays[1];

            if (index == "add") {
                arr.push(name);

            } else if (index === "delete" || index === "del") {
                arr = arr.filter(n => n !== name)

            }
            if (index === "stop") {
                break;
            }
        }
        console.log(arr);
    </script>
</body>

</html>
BladeOfLightX
  • 504
  • 4
  • 17