javascript - 反应过滤器然后交换数组元素
问题描述
const filterData = apiData.filter(data => {
return this.shouldDisplayItem(
data,
[this.state.searchValue],
this.state.filterKeyValue
);
}).filter(i => i.vid),
x = 0,
y = apiData.map(i => i.vid).indexOf(markerId);
A[x] = A.splice(y, 1, A[x])[0];
例如,我有一个array = [0,1,2,3,4,5,6,7,8,9]
. 首先我想过滤大于 2 的值,然后我想通过索引号交换 7 和 8。
在最初的原始项目中,我正在做一些过滤器而不是第二个过滤器我正在交换两个数组对象我们可以一次过滤两次相同的数组吗?
解决方案
您可以使用filter
过滤掉数组,然后swap
使用prototype
Array.prototype.swap = function (swapFirst,swapSecond) {
var x = this.findIndex(a=> a === swapFirst);
var y = this.findIndex(a=> a === swapSecond);
var b = this[y];
this[y] = this[x];
this[x] = b;
return this;
}
var apiData = [0,1,2,3,4,5,6,7,8,9];
var filtered= apiData.filter(a=> a > 2).swap(7,8);
console.log(filtered);
推荐阅读
- docker - 码头工人无法启动
- apache-spark - 将参数传递给 Spark Insert 脚本
- arrays - 在定时行删除时始终使索引越界异常
- php - 如何在没有容器流体的情况下修复导航栏碰撞?
- sql - 如何使用 instr() 搜索多个子字符串
- outlook-addin - 如何在用户打开 Outlook 时默认设置自定义加载项的固定
- angular - 如何使用 runGuardsAndResolvers 和 onSameUrlNavigation 在 Angular 中重新运行解析器
- php - 仅当用户输入行数和列数时,是否有关于如何打印模式的想法?
- javascript - 订单质量
- node.js - 请求重新调整可能的 gzip