简体   繁体   English

单击按钮从数组中删除对象

[英]Deleting an object from an array on click of a button

I am trying to create my own small Twitter.我正在尝试创建自己的小型 Twitter。

It is all working fine but I cannot find a way to delete specific tweet on click of a button.一切正常,但我找不到通过单击按钮删除特定推文的方法。 I have tried splice() but it deletes the first object of an array always.我试过 splice() 但它总是删除数组的第一个对象。

Here is my code:这是我的代码:

 var tweets = [] function postNewTweet() { var today = new Date(); var date = today.getDate() + '-' + (today.getMonth() + 1) + '-' + today.getFullYear(); var time = today.getHours() + ':' + today.getMinutes(); var id = tweets.length + 1; var li = document.createElement('li'); var inputValue = document.getElementById('newTweet').value; var finalValue = id + ' ' + inputValue + ' ' + date + ' ' + time; var t = document.createTextNode(finalValue); li.appendChild(t); tweets.push({ id: id, content: inputValue, date: date + ' ' + time }); document.getElementById('list').appendChild(li); document.getElementById('newTweet').value = ""; console.log(tweets); var buttonDelete = document.createElement("button"); buttonDelete.innerHTML = '<i class="far fa-trash-alt"></i>'; buttonDelete.onclick = deleteItem; function deleteItem(e) { var ul = document.getElementById('list'); ul.removeChild(li); var list = document.getElementById('list'); list.addEventListener('click', function(e) { var index = e.target.getAttribute('value'); tweets.splice(index, 1); console.log(tweets) }); } li.appendChild(buttonDelete); }
 <div id='post'> <textarea maxlength="160" id='newTweet'></textarea> <button id='postIt' onclick="postNewTweet()">Post</button> </div> <ul id='list'> </ul>

So it deletes it in HTML, but not in array correctly.所以它会在 HTML 中删除它,但不会正确地在数组中删除它。

There are two issues:有两个问题:

  1. If you just take the length of the array as the id you will get duplicate entries, if you delete an entry.如果您只是将数组的长度作为 id,那么如果您删除一个条目,您将获得重复的条目。 Perhaps go to a timestamp - i just used the one you already had there but added seconds也许去一个时间戳 - 我只是使用了你已经在那里但增加了几秒钟的时间戳
  2. You retrieve the value-attribute but for splice you need the index of the element.您检索 value-attribute 但对于 splice 您需要元素的索引。 I just added the timestampt as an attribute to the button and used it for removal.我只是将时间戳作为属性添加到按钮并将其用于删除。

Probably not my best code but I hope it gives you the right hints.可能不是我最好的代码,但我希望它能给你正确的提示。

 var tweets = [] function postNewTweet() { var today = new Date(); var date = today.getDate() + '-' + (today.getMonth() + 1) + '-' + today.getFullYear(); var time = today.getHours() + ':' + today.getMinutes() + ':' + today.getSeconds(); var id = tweets.length + 1; var li = document.createElement('li'); var inputValue = document.getElementById('newTweet').value; var finalValue = id + ' ' + inputValue + ' ' + date + ' ' + time; var t = document.createTextNode(finalValue); li.appendChild(t); tweets.push({ id: id, content: inputValue, date: date + ' ' + time }); document.getElementById('list').appendChild(li); document.getElementById('newTweet').value = ""; console.log(tweets); var buttonDelete = document.createElement("button"); buttonDelete.innerHTML = '<i class="far fa-trash-alt" del-date="'+date + ' ' + time +'">del</i>'; buttonDelete.onclick = deleteItem; function deleteItem(e) { var ul = document.getElementById('list'); ul.removeChild(li); var list = document.getElementById('list'); list.addEventListener('click', function(e) { var delDate = e.target.getAttribute('del-date'); let index = tweets.map((item) => item.date).indexOf(delDate); console.log(index); tweets.splice(index, 1); console.log(tweets) }); } li.appendChild(buttonDelete); }
 <div id='post'> <textarea maxlength="160" id='newTweet'></textarea> <button id='postIt' onclick="postNewTweet()">Post</button> </div> <ul id='list'> </ul>

As you have access to li in your delete function, you have access to all the other data too.由于您可以在删除功能中访问li ,因此您也可以访问所有其他数据。 You can use them to find out the element to remove from the tweets array.您可以使用它们找出要从tweets数组中删除的元素。 For example, in your current code, you can use the id :例如,在您当前的代码中,您可以使用id

    tweets.splice(id - 1, 1)

Or you can use filter with any of the data that you store in tweets .And I don't see any use for this part:或者,您可以对存储在tweets任何数据使用filter 。我看不出这部分有任何用处:

var list = document.getElementById('list');
    list.addEventListener('click', function(e) {
      var index = e.target.getAttribute('value');
      tweets.splice(index, 1);
      console.log(tweets)
    });

You can just remove the tweet under the ul.removeChild您可以删除ul.removeChild下的推文

The second part of your deleteItem function's body seems useless. deleteItem函数主体的第二部分似乎没用。 While there are couple of ways to resolve it, I suggest the following:虽然有几种方法可以解决它,但我建议如下:

function deleteItem(e) {
    var ul = document.getElementById('list');
    ul.removeChild(li);

    var foundIndex = tweets.findIndex(function (tweet) {
        return tweet.id == id;
    });
    if (foundIndex > -1) {
        tweets.splice(foundIndex, 1);
    }
}

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

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