简体   繁体   English

如何从数组中删除特定索引,然后在同一索引上插入一个新索引?

[英]How can I remove a specific index from an array and then insert a new one on that same index?

I am trying to do one thing but I have 2 choices我正在尝试做一件事,但我有 2 个选择

  1. Remove a specific item/index from an array and then insert a new one in that same index.从数组中删除特定项/索引,然后在同一索引中插入一个新项/索引。
  2. Do not remove the item but edit it with new data.不要删除该项目,而是使用新数据对其进行编辑。

I have this piece of code:我有这段代码:

const handleClick = () => {
    for (let i = 0; i < qs.length; i++) {
      if (qs[i].id === getNewQ.id) {
        const qsCopy = [...qs].filter((q) => q.id !== getNewQ.id);

        return [...qsCopy, { ...newGroup }];
      }
    }

    return [...qs];
  };

It removes the item from the array, but when it adds the new item, it adds it to the last index.它从数组中删除该项目,但是当它添加新项目时,它会将其添加到最后一个索引中。 But what I need, is this new item to be added to the index where the other item was removed.但我需要的是,将这个新项目添加到删除另一个项目的索引中。

Is it clear?清楚吗?

I created a reproducible example: https://codesandbox.io/s/pedantic-darwin-4tty0?file=/src/App.tsx:912-1171我创建了一个可重现的示例: https://codesandbox.io/s/pedantic-darwin-4tty0?file=/src/App.tsx:912-1171

In the sample, you will notice how the new item gets added to the last index.在示例中,您会注意到新项目是如何添加到最后一个索引的。 It needs to be as generic as possible because it is dynamic.它需要尽可能通用,因为它是动态的。 Not all the time would be the same index.并非所有时间都是相同的索引。

Map the qs instead, and when the ID matches, return the newGroup instead of the previous item in state: Map qs代替,当 ID 匹配时,返回newGroup代替 state 中的前一项:

const handleClick = () => qs.map((q) => (
  q.id === getNewQ.id
    ? { ...newGroup }
    : q
));

To avoid iterating over the whole array use findIndex as follow为了避免遍历整个数组,使用 findIndex 如下

const handleClick = () => {
  const qsCopy = [...qs];
  const itemIndex = qs.findIndex(({ id }) => (id === getNewQ.id));

  if (itemIndex !== -1) {
    qsCopy[itemIndex] = { ...newGroup };
  }

  return qsCopy;
};

CertainPerformance provided a good solution allowing you to only foreach your array once. CertainPerformance 提供了一个很好的解决方案,允许您只对数组进行一次 foreach。

Just for informations maybe would be useful for other people:仅作为信息可能对其他人有用:

There are other ways to do it as well You could have done a find of your object and then add your properties.还有其他方法可以做到这一点你可以找到你的 object 然后添加你的属性。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find Like: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find喜欢:

const myObject = qs.find(q => q.id === myId)

But a spread wouldn't have worked because it would create another object.但是传播是行不通的,因为它会创建另一个 object。

Another possibility: A findIndex and a splice if needed另一种可能性:如果需要,一个 findIndex 和一个拼接

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects /数组/查找索引

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

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