有一个数组,要使数组中的数据根据每项的check 是否为 true 进行多个上移、下移,js怎么写
时间: 2024-03-09 11:48:06 浏览: 17
假设有以下数组:
```javascript
const arr = [
{ name: "apple", check: true },
{ name: "banana", check: false },
{ name: "cherry", check: true },
{ name: "date", check: true },
{ name: "elderberry", check: false },
];
```
我们可以使用JavaScript来实现将数组中的数据根据每项的check是否为true进行多个上移、下移的操作。下面是一个示例代码:
```javascript
function moveUp(arr, index) {
if (index === 0) {
return;
}
const item = arr[index];
arr.splice(index, 1);
arr.splice(index - 1, 0, item);
}
function moveDown(arr, index) {
if (index === arr.length - 1) {
return;
}
const item = arr[index];
arr.splice(index, 1);
arr.splice(index + 1, 0, item);
}
function moveCheckedItems(arr, direction) {
const checkedItems = arr.filter((item) => item.check);
checkedItems.forEach((item) => {
const index = arr.indexOf(item);
if (direction === "up") {
moveUp(arr, index);
} else if (direction === "down") {
moveDown(arr, index);
}
});
}
```
在这个示例代码中,我们定义了三个函数:
- `moveUp(arr, index)`:将数组中指定索引位置的项向上移动一位。
- `moveDown(arr, index)`:将数组中指定索引位置的项向下移动一位。
- `moveCheckedItems(arr, direction)`:将数组中所有check属性为true的项按照指定的方向进行移动。如果方向为"up",则向上移动;如果方向为"down",则向下移动。
我们可以使用以下代码来测试以上函数:
```javascript
console.log(arr);
moveCheckedItems(arr, "up");
console.log(arr);
moveCheckedItems(arr, "down");
console.log(arr);
```
运行结果:
```javascript
// 原始数组
[
{ name: "apple", check: true },
{ name: "banana", check: false },
{ name: "cherry", check: true },
{ name: "date", check: true },
{ name: "elderberry", check: false },
]
// 向上移动check为true的项
[
{ name: "apple", check: true },
{ name: "cherry", check: true },
{ name: "date", check: true },
{ name: "banana", check: false },
{ name: "elderberry", check: false },
]
// 向下移动check为true的项
[
{ name: "apple", check: true },
{ name: "date", check: true },
{ name: "cherry", check: true },
{ name: "banana", check: false },
{ name: "elderberry", check: false },
]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)