简体   繁体   English

从数组中删除特定元素-JavaScript

[英]Delete specific element from array - javascript

I wanted to create small program where you enter the Book name and it's isbn code, with some other stuff, and it worked just fine until I wanted to create a prompt that asks me which book i want to delete (by it's ISBN). 我想创建一个小程序,在其中输入``书名''和isbn代码以及其他内容,在我想创建一个提示询问我要删除哪本书(通过ISBN)之前,它工作得很好。 It actually works fine; 实际上工作正常; when I write the isbn of the book that i stored in array, it deletes that book and it's isbn, but the alert is notifying me that the book does not exist in the database (not added previously) for every book I have stored. 当我写我以阵列形式存储的书的isbn时,它将删除该书,它就是isbn,但是警报通知我该书不存在于我已存储的每一本书的数据库中(之前未添加)。

For example, I have 5 books stored, one is (4th object in array) with "12121" ISBN code and I want to delete that one from the array. 例如,我存储了5本书,其中一本书是(数组中的第4个对象)带有“ 12121” ISBN代码,我想从数组中删除该本书。 Function returns me false for first 3 objects in array (alert("Book with that ISBN doesn't exist in our database.")), then it returns true for the 4th object and then it returns me false also for the last object in array. 函数对数组中的前3个对象返回false(alert(“在我们的数据库中不存在带有该ISBN的书。”)),然后对第四个对象返回true,然后对其中的最后一个对象返回false。数组。

How do I make the function that only selects (and then deletes) object with ISBN that I put there in prompt box without checking every one of the objects inside the array? 如何制作仅选择(然后删除)带有ISBN的对象的函数,该对象放在提示框中,而无需检查数组中的每个对象?

    var book = [];
book.push({
    bookName: "GameOfThrones",
    isbn: "12345",
});
function deleteBook(){
    var book1 = prompt("Enter the ISBN of the book you want to delete");
    for var(i=0; i<book.length; i++){
        if (book[i].isbn == book1){
            book.splice(i, 1);
            alert("Book is successfully deleted");
        }
        else{
            alert("Book with that ISBN doesn't exist in our database.");
        }
    }
    for (var i=0; i<book.length; i++){
        document.write(book[i].isbn + " - " + book[i].bookName + "<br/>");
    }
}

If you choose to use an array of elements at random, then there is no getting around looking through each element. 如果您选择随机使用元素数组,那么遍历每个元素将无处可寻。 Your other option is to use a hash map or sort your array and use the binary search algorithm. 您的另一个选择是使用哈希图或对数组进行排序,然后使用二进制搜索算法。 Personally, I wouldn't bother with such premature optimization unless your Array is very very big (an order of a hundred thousand elements). 就个人而言,除非您的Array非常大(十万个元素的数量级),否则我不会为这样的过早优化而烦恼。

As an aside, your original code can be written in a much cleaner manner if you make use of the Array filter function . 顺便说一句,如果您使用Array过滤器功能,则可以以更简洁的方式编写原始代码。

    var books = [];
    books.push({
        bookName: "GameOfThrones",
        isbn: "12345",
    });

    var ISBNToDelete = prompt("Enter the ISBN of the book you want to delete");

    function deleteBookByISBN(isbn, books) {
        var newBooks = books.filter(function(book) {
           return book.isbn !== isbn;
        });
        if (newBooks.length != books.length) {
             alert("Book successfully deleted")  
        } else {
             alert("Book with that ISBN doesn't exist in our database.");
        }
        return newBooks;
    }
    books = deleteBookByISBN(ISBNToDelete, books); // reassign the new set of books to books.

Easiest way with your example is to use break statement to stop processing of the for loop once you've found item: 您的示例最简单的方法是找到项目后,使用break语句停止处理for循环:

 function deleteBook(){
     var book1 = prompt("Enter the ISBN of the book you want to delete");
     for (var i=0,count=book.length; i<count; i++){
         if (book[i].isbn == book1){
             book.splice(i, 1);
             alert("Book is successfully deleted");
             break;
         }
         else{
             alert("Book with that ISBN doesn't exist in our database.");
         }
     }
     for (var i=0; i<book.length; i++){
         document.write(book[i].isbn + " - " + book[i].bookName + "<br/>");
     } 
}

I don't know what browsers you're targeting, but if it's not IE you can eliminate the for loop and use Array.findIndex instead: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex 我不知道您要定位的浏览器,但是如果不是IE,则可以消除for循环并改用Array.findIndexhttps : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/参考/ Global_Objects / Array / findIndex

Something like: 就像是:

var index = book.findIndex(function (element) { return element.isbn == book1 });

You could simply return from for statement at the moment you have found your book. 找到书后,您可以简单地从for语句返回。 The problem is when book is at the end of an array, in that case you would go trough whole array anyway. 问题是当书在数组的末尾时,在这种情况下,您无论如何都要遍历整个数组。

Sorted array could help you a little bit (you would know approximately where to look for the book), but the best thing to do is to ditch the array and choose some kind of Hash table. 排序数组可以为您提供一些帮助(您大概会知道在哪里找书),但是最好的办法是抛开数组并选择某种哈希表。

Easy implementation would be: 容易实现的是:

var books = {};
books['UniqueISBN'] = {name: "Where is the sun", author: "Unknown"};

Then you can delete it directly using delete; 然后,您可以使用delete直接将其删除;

var id = 'UniqueISBN';
delete books[id];

If you filter on the array, you can return all the books that didn't match. 如果您对数组进行filter ,则可以返回所有不匹配的书。 During that process, you can store a boolean if the book was found. 在此过程中,如果找到了书,则可以存储一个布尔值。 Using that boolean, you can perform other actions (like deleting it from the database). 使用该布尔值,您可以执行其他操作(例如,从数据库中删除它)。

You could perform those database actions inside the filter , but to keep code easier to follow and maintain, it's good to separate it. 您可以在filter执行这些数据库操作,但是为了使代码更易于跟踪和维护,最好将它们分开。 Which is why there's an if -block after the filter . 这就是为什么在filter之后有一个if -block的原因。

This following example demonstrates this concept above. 下面的示例演示了上述概念。 It also uses a textarea for the logging, as opposed to the console. 与控制台相反,它也使用textarea进行日志记录。 This is meant to serve as a demonstration on how to output to an HTML element using the textContent property. 这旨在作为演示如何使用textContent属性输出到HTML元素。

Example: 例:

 document.addEventListener("DOMContentLoaded", onLoad); var books = [{ bookName: "GameOfThrones", isbn: "12345" }, { bookName: "Jurrasic Park", isbn: "98765" }, { bookName: "Westworld", isbn: "33333" }]; function deleteBook(isbn) { // use isbn passed in, or look it up isbn = isbn || document.getElementById('isbn').value; var book_found = false; books = books.filter((book, i) => { if (book.isbn == isbn) { book_found = true; debug.textContent += `Deleting "${book.bookName} from database...\\n`; return false; } else return isbn != book.isbn; }); // handle any follow-up actions if (book_found) { debug.textContent += `New List (w/o ${isbn}):\\n`; outputBooks(books); } else debug.textContent += `Book (isnb:${isbn}) could not be found!\\n\\n`; } // Used for Logging Output (in place of console) function outputBooks(books) { books.forEach(book => { debug.textContent += [' ' + book.isbn, book.bookName].join(': ') + '\\n' }); debug.textContent += '\\n'; } // Ensures the debug element is on the page function onLoad(event) { // Default window.debug = document.getElementById('debug'); debug.textContent += 'Starting List:\\n'; outputBooks(books); } 
 textarea { height: 20em; width: 80%; display: block; } .block { display: block; } 
 <label>ISBN: <input id="isbn" type="text" /> </label> <button type="button" class="delete" onclick="deleteBook()">Delete</button> <label><span class="block">Debug:</span> <textarea id="debug" disabled></textarea> </label> 

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

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