简体   繁体   English

如何从对象内部的数组中删除特定元素?

[英]How to remove a specific element from an array which is inside of an object?

I am learning how to manage application state via a shopping list exercise. 我正在学习如何通过购物清单练习来管理应用程序状态。 Per the instructions, I have an array inside an object where I store any items I add: 按照说明,我在对象内部有一个数组,用于存储添加的所有项目:

var state = {
    items: []
};

To modify state I use this function: 要修改state ,请使用以下功能:

var addItem = function(state, item) {
state.items.push(item);
};

which is called later via an event listener (and added to the DOM via renderList , not shown here): 稍后通过事件监听器调用(并通过renderList添加到DOM,此处未显示):

$('#js-shopping-list-form').submit(function(event){
    event.preventDefault();
    addItem(state, $('#shopping-list-entry').val());
    renderList(state, $('.shopping-list'));
});

How can I remove a specific item from the array inside my state object? 如何从state对象内的数组中删除特定项目? Essentially I want to reverse the sequence above when the user clicks on <button class="shopping-item-delete"> . 本质上,当用户单击<button class="shopping-item-delete">时,我想颠倒上述顺序。

Here's a demo of the final solution: https://thinkful-ed.github.io/shopping-list-solution/ 这是最终解决方案的演示: https : //thinkful-ed.github.io/shopping-list-solution/

HTML HTML

<body>

  <div class="container">

    <form id="js-shopping-list-form">
      <label for="shopping-list-entry">Add an item</label>
      <input type="text" name="shopping-list-entry" id="shopping-list-entry" placeholder="e.g., broccoli">
      <button type="submit">Add item</button>
    </form>

    <ul class="shopping-list">
    <li>
        <span class="shopping-item">apples</span>
        <div class="shopping-item-controls">
          <button class="shopping-item-toggle">
            <span class="button-label">check</span>
          </button>
          <button class="shopping-item-delete">
            <span class="button-label">delete</span>
          </button>
        </div>
      </li>
    </ul>
  </div>

  <script
  src="https://code.jquery.com/jquery-3.1.1.min.js"
  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  crossorigin="anonymous"></script>
<script type="text/javascript" src="app.js"></script>
</body>

You could create a function as follows: 您可以创建一个函数,如下所示:

var deleteItem = function(state, item) {
    var index = state.items.indexOf(item);
    if (index > -1) {
        state.items.splice(index, 1);
    }
};

Note that the method indexOf is not supported in Internet Explorer 7 and 8. 请注意,Internet Explorer 7和8不支持indexOf方法。

If you know the index of the item you can use. 如果您知道该项目的索引,则可以使用。 You can determine the index by value of item 您可以通过项目的值确定索引

state.items.splice(indexOfItemToRemove, 1);

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

There are a couple of ways to remove elements from Arrays: 有两种方法可以从数组中删除元素:

shift will remove the first item from the beginning shift将删除开头的第一项

pop will remove the last item from the end. pop将从末尾删除最后一个项目。

splice allows you to remove an element at a desired location splice使您可以在所需位置删除元素

Note that all of these will modify the original array (they work "in-place") as opposed to returning a new array. 请注意,所有这些都将修改原始数组(它们在“原位”工作),而不是返回新数组。

You may loop over the items 您可以在项目上循环

var removeItem = function(state, item) {
  for(var i = state.items.length - 1; i >= 0; i--) {
    if(state.items[i] === item) {
       state.items.splice(i, 1);
       break;
    }
  }
};

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

相关问题 如何从ImmutableJS中的对象内部的数组中删除元素 - How to delete an element from an array which is inside an object in ImmutableJS 如何从所有对象中删除一个简单的特定键值对并添加到数组内的一个特定 object - How to remove a simple specific key value pair from all objects and add to one specific object inside an array 如何从JavaScript中的数组元素中删除对象 - how to remove object from array element in javascript 从 javascript 中的 JSON object 中删除特定数组元素 - Remove specific array element from JSON object in javascript 从 object 中的数组中删除除特定项之外的所有项目 - Remove all items except specific from array inside object 如何使用ES6扩展运算符和rest运算符从数组或对象中删除特定元素 - How to remove specific element from a array or object using ES6 spread operators and rest operators 如何从javascript或jquery中的元素数组中删除特定元素 - How to remove specific element from element array in javascript or jquery 如何从 React JS (ES6) 中 object 中的数组中删除元素 - How to remove an element from an array that is inside an object in React JS (ES6) 如何从数组中对象内部的数组中删除此对象? - how to remove this object from array inside of object in array? 如何从可观察数组内部的可观察数组中删除项目 - How to remove an item from an observable array which is inside an observable array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM