【问题标题】:Move array objects/elements Up and Down by index按索引上下移动数组对象/元素
【发布时间】:2018-11-29 07:19:04
【问题描述】:

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

<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"}
]

数组值怎么可能..

【问题讨论】:

  • 好吧,您不应该用答案中的代码更新您的问题,这会令人困惑,但无论如何,我看不到 moveUp()moveDown() 函数与按钮的任何绑定在您的代码中。
  • 我将更新我的第一个编码,我的主要问题是在表格中显示这个数组值。如何用这个函数定义表中的数组值,这个上下函数在点击时执行。

标签: javascript jquery arrays object


【解决方案1】:

如果您想对array 执行相同的操作,您只需检查给定的element 是否具有previousnext 元素,这样您就可以交换两个对象以避免an index out of bound.

这应该是你的代码:

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;
  }
}

要在arrayelement 上移,您需要确保此element 不是array 中的第一个 元素,然后执行交换操作。

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;
  }
}

element 向下移动,您需要确保此element 不是array 中的最后一个

演示:

这是一个有效的演示示例:

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);

【讨论】:

  • 它工作正常,但我想在表格中显示所有数组值,每行都有向上和向下按钮。基于单击行中的按钮来更改表格中的上一个和下一个位置..
  • @NewUSer 我没有任何淘汰赛的经验,但您所要做的就是正确地将id 传递给moveUp()moveDown 函数。
  • 好的,如何使用带有单击功能的 jquery 在表中显示此数组值。是否还有其他方法可以绑定表中的数据..
  • 请更新您的问题,使用相关的敲除代码,您已经尝试过了。通常,如果您从 Knockout 代码中调用函数,它将动态处理渲染。
猜你喜欢
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多