如何按位置插入和删除数组元素,同时更新位置

huangapple go评论63阅读模式
英文:

How to insert and delete elements from an array by position, while updating positions

问题

I have an array of objects with a position property along with other properties like this:

[{position: 1, ...otherProperties}, ...otherObjects]

In the frontend, the objects are displayed and sorted by their position.

I am looking for JavaScript functions to perform the following operations:

  1. Insert a new object at a specified position (e.g., before the element with position: 1) and update the positions of the other elements accordingly (e.g., previous position: 1 element will now be position: 2).
  2. Delete an object from a specified position and update the positions of the remaining elements accordingly.

I am struggling with the creation of these functions.

英文:

I have an array of objects with a position property along with other properties like this:

[{position: 1, ...otherProperties}, ...otherObjects]

In the frontend, the objects are displayed and sorted by their position.

I am looking for JavaScript functions to perform the following operations:

  1. Insert a new object at a specified position (e.g., before the element with position: 1) and update the positions of the other elements accordingly (e.g., previous position: 1 element will now be position: 2).
  2. Delete an object from a specified position and update the positions of the remaining elements accordingly.

I am struggling with the creation of these functions

答案1

得分: 1

You can create two functions addElement and removeElement to add or delete elements in your array while ensuring the positions are sorted correctly. For example:

function addElement(arr, newPosition, newElement) {
  // Add a new element at the given position
  newElement.position = newPosition;
  arr.push(newElement);

  // Sort the array by position
  arr.sort((a, b) => a.position - b.position);

  // Update the position of elements
  arr.forEach((item, index) => {
    item.position = index + 1;
  });

  return arr;
}

function removeElement(arr, positionToRemove) {
  // Remove the element from the given position
  arr = arr.filter(item => item.position !== positionToRemove);

  // Update the remaining elements' positions
  arr.forEach((item, index) => {
    item.position = index + 1;
  });

  return arr;
}

Usage:

let array = [
  { position: 1, prop: "a" },
  { position: 2, prop: "b" },
  { position: 3, prop: "c" },
];

let newArray = addElement(array, 1, { prop: "d" });
console.log(newArray);

newArray = removeElement(newArray, 3);
console.log(newArray);
英文:

You can create two functions addElement and removeElement to add or delete elements in your array, while ensuring the positions are sorted correctly. For example:

function addElement(arr, newPosition, newElement) {
  // Add a new element at the given position
  newElement.position = newPosition;
  arr.push(newElement);

  // Sort the array by position
  arr.sort((a, b) => a.position - b.position);

  // Update the position of elements
  arr.forEach((item, index) => {
    item.position = index + 1;
  });

  return arr;
}

function removeElement(arr, positionToRemove) {
  // Remove the element from the given position
  arr = arr.filter(item => item.position !== positionToRemove);

  // Update the remaining elements' positions
  arr.forEach((item, index) => {
    item.position = index + 1;
  });

  return arr;
}

Usage:

let array = [
  { position: 1, prop: "a" },
  { position: 2, prop: "b" },
  { position: 3, prop: "c" },
];

let newArray = addElement(array, 1, { prop: "d" });
console.log(newArray);

newArray = removeElement(newArray, 3);
console.log(newArray);

答案2

得分: 1

你需要一个方法来解析数组中的所有项并为它们设置新的位置。

function fixPosition(arr) {
  arr.map((o, i) => o.pos = i+1)
  return arr
}
英文:

You need a method that parse all items of your array and set the new position to all of it.

function fixPosition(arr) {
  arr.map((o, i) => o.pos = i+1)
  return arr
}

huangapple
  • 本文由 发表于 2023年4月11日 04:01:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定