javascript - 按索引上下移动数组对象/元素

标签 javascript jquery arrays object

我有一个带有向上和向下按钮的值列表。如果我想单击向上按钮,则元素会以列表中的先前值向上移动,然后单击向下按钮,它们会向下移动到列表中的下一项。我的示例代码在这里,

<ul>
  <li> 1 &nbsp;&nbsp;<button class="up">UP</button>&nbsp;&nbsp;<button class="down">DOWN</button></li>
  <li> 2 &nbsp;&nbsp;<button class="up">UP</button>&nbsp;&nbsp;<button class="down">DOWN</button></li>
  <li> 3 &nbsp;&nbsp;<button class="up">UP</button>&nbsp;&nbsp;<button class="down">DOWN</button></li>
  <li> 4 &nbsp;&nbsp;<button class="up">UP</button>&nbsp;&nbsp;<button class="down">DOWN</button></li>
  <li> 5 &nbsp;&nbsp;<button class="up">UP</button>&nbsp;&nbsp;<button class="down">DOWN</button></li>
</ul>

<script type="text/javascript">
function moveUp(element) {
  if(element.previousElementSibling)
    element.parentNode.insertBefore(element, element.previousElementSibling);
}
function moveDown(element) {
  if(element.nextElementSibling)
    element.parentNode.insertBefore(element.nextElementSibling, element);
}
document.querySelector('ul').addEventListener('click', function(e) {
  if(e.target.className === 'down') moveDown(e.target.parentNode);
  else if(e.target.className === 'up') moveUp(e.target.parentNode);
});
    </script>

这是带有要显示的值列表的代码,但我希望以这种格式显示数组值,该格式根据索引执行向上和向下功能。 我的数组元素是:

[
    { id: "Racer-101", rank: "1"},
    { id: "Racer-102", rank: "2"},
    { id: "Racer-103", rank: "3"},
    { id: "Racer-104", rank: "4"},
    { id: "Racer-105", rank: "5"},
    { id: "Racer-106", rank: "6"},
    { id: "Racer-107", rank: "7"},
    { id: "Racer-108", rank: "8"},
    { id: "Racer-109", rank: "9"}
]

数组值怎么可能..

最佳答案

如果您想对数组执行相同的操作,您所需要做的就是检查给定的元素是否有前一个 > 或 next 元素,以便您可以交换两个对象以避免索引越界

这就是您的代码:

function moveUp(id) {
  let index = arr.findIndex(e => e.id == id);
  if (index > 0) {
    let el = arr[index];
    arr[index] = arr[index - 1];
    arr[index - 1] = el;
  }
}

要在数组向上移动元素,您需要确保此元素 不是数组中的第一个元素,则执行交换操作。

function moveDown(id) {
  let index = arr.findIndex(e => e.id == id);
  if (index !== -1 && index < arr.length - 1) {
    let el = arr[index];
    arr[index] = arr[index + 1];
    arr[index + 1] = el;
  }
}

向下移动元素,您需要确保此元素不是最后一个数组中。

演示:

这是一个工作演示示例:

var arr = [
    { id: "Racer-101", rank: "1"},
    { id: "Racer-102", rank: "2"},
    { id: "Racer-103", rank: "3"},
    { id: "Racer-104", rank: "4"},
    { id: "Racer-105", rank: "5"},
    { id: "Racer-106", rank: "6"},
    { id: "Racer-107", rank: "7"},
    { id: "Racer-108", rank: "8"},
    { id: "Racer-109", rank: "9"}
];

function moveUp(id) {
  let index = arr.findIndex(e => e.id == id);
  if (index > 0) {
    let el = arr[index];
    arr[index] = arr[index - 1];
    arr[index - 1] = el;
  }
}

function moveDown(id) {
  let index = arr.findIndex(e => e.id == id);
  if (index !== -1 && index < arr.length - 1) {
    let el = arr[index];
    arr[index] = arr[index + 1];
    arr[index + 1] = el;
  }
}
moveDown("Racer-101");
moveUp("Racer-103");
console.log(arr);

关于javascript - 按索引上下移动数组对象/元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53533715/

相关文章:

javascript - 表单验证在使用 Angular JS 和 Node JS 的 MEAN Stack 中不起作用

javascript - 无法获取未定义或空引用的属性 - Windows 8 JS/CSS 应用程序

javascript - 从ajax响应增加文本框宽度值不起作用

javascript - JavaScript 中的串联

javascript - 重新加载页面并转到之前事件的选项卡 php jquery-jtable

javascript - 添加样式表但不添加样式

php - 根据列值对数据行进行分组

python - 如何自动增加 P​​ython 中索引常量列表定义的值?

PHP/SQL 将查询结果分组到数组中

java - 如何在java spring MVC中为相应行插入按钮功能