简体   繁体   English

从阵列中删除 object?

[英]Remove object from an array?

remove an object from an array is not working properly,but it add object perfectly从阵列中删除 object 无法正常工作,但它完美地添加了 object

const addItem =(selected)=>{
let data = selectedItems ? [...selectedItems] : [];
if (data.length) {
  let index = data.indexOf(selected);
  console.log(index);
  if (index !== -1) {
    data.splice(index, 1);
    setSelectedItems(data);
  } else {
    data.push(selected);
  }
} else {
  data.push(selected);
}
console.log("selected", selectedItems);

setSelectedItems(data);
}

render button function add or remove on click it渲染按钮 function 添加或删除点击它

<div className="file-list">
  <MappedElement 
   data={[{ _id: 1 }, { _id: 2 }]}  
   renderElement={(value, index, arr) => { 
   let check=selectedItems.some((obj) => obj._id === value._id); 
   console.log("check", check); 
   return ( 
    <DocumentCard key={index} className={file-list-item ${check ? 
       "active" : ""}} 
    onClick={() => addItem(value, arr, index)} /> ); }} /> 
 </div> 

Use利用

const addItem =(selected)=>{
let data = selectedItems ? [...selectedItems] : [];
if (data.length) {
  let index = data.findIndex(value => value._id === selected._id)ж
  console.log(index);
  if (index !== -1) {
    data.splice(index, 1);
  } else {
    data.push(selected);
  }
} else {
  data.push(selected);
}
console.log("selected", selectedItems);

setSelectedItems(data);
}

Your problem is probably in indexOf method you're using.您的问题可能出在您正在使用的indexOf方法中。

You can not use this to find objects in your array.您不能使用它来查找数组中的对象。

There are several options you can use.您可以使用几个选项。 You can use find or findIndex and pass a callback to find an object by the specified (preferably unique property of the object).您可以使用 find 或 findIndex 并传递回调以通过指定(最好是对象的唯一属性)查找 object。

Example例子

let found = data.findIndex(item => item.id === selected.id);

For a selectedItems array that looks like:对于如下所示的selectedItems数组:

const selectedItems = [
    { _id: 1, /* other fields */ },
    { _id: 2, /* other fields */ },
    { _id: 3, /* other fields */ },
    /* other objects */
];

And a selected object that looks like:和一个selected的 object 看起来像:

const selected = { _id: 1 };

In order to perform the desired behavior, which is: if the element exists, remove it, else, add it , you can write the following:为了执行所需的行为,即:如果元素存在,则删除它,否则,添加它,您可以编写以下内容:

// copy selected items with a fail-safe empty array
const data = selectedItems ? [...selectedItems] : [];
// find index of selected element
const removalIndex = data.findIndex(({ _id }) => (_id === selected._id));
// if selected element exists in data array, remove it
if (removalIndex !== -1) {
    data.splice(removalIndex, 1);
}
// if selected element doesn't exist in data array, add it
else {
    data.push(selected);
}
// update selected elements
setSelectedItems(data);

NOTE: if your array of selected items contains duplicates, meaning multiple objects that contain the same value for _id , then this approach will be removing only the first instance of those.注意:如果您的选定项目数组包含重复项,即多个对象包含相同的_id值,则此方法将仅删除其中的第一个实例。 If you want to remove all of them, you'll have to use a loop or recursivity.如果要删除所有这些,则必须使用循环或递归。

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

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