当前位置:网站首页>JS数组过滤
JS数组过滤
2022-08-01 19:19:00 【网瘾颓废】
每五个组成一个数组
var spArray = function(N,Q)
{
var R = [],F;
for (F = 0;F < Q.length;) {
R.push(Q.slice(F,F += N))
}
return R
}
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
console.log(spArray(5,arr))
根据一个数组 过滤出另一个数组
var aim = [
{name:‘Anne’, age: 23, gender:‘female’},
{name:‘Leila’, age: 16, gender:‘female’},
{name:‘Jay’, age: 19, gender:‘male’},
{name:‘Mark’, age: 40, gender:‘male’}
]
var nameArr=[‘Leila’,‘Jay’]
// 根据多个名字筛选
function filterByName1(aim, nameArr) {
let newArray = []
aim.forEach(item => { //遍历arr
//此处如果需要留下重复元素,则将!去掉即可
//arr中的元素如果在ids中不存在,则添加到新数组中
if (!nameArr.includes(item.name)) {
newArray.push(item);
};
})
return newArray
}
console.log(filterByName1(aim,nameArr))
多条件过滤
let arr = [{
name: "q1121",
age: 12,
address: "辽宁大连1"
},
{
name: "q44",
age: 12,
address: "辽宁大连4"
},
{
name: "q55",
age: 12,
address: "辽宁大连5"
},
{
name: "q222",
age: 13,
address: "辽宁大连2"
},
{
name: "q332",
age: 12,
address: "辽宁沈阳1"
}
];
//筛选条件
let filter = {
name: "",
age: 12,
address: "辽宁",
};
//拿到有值的参数
let tempFilter = {};
for(key in filter) {
if(typeof(filter[key]) != "undefined" && typeof(filter[key]) != "null" && filter[key] != null && filter[key] != "") {
tempFilter[key] = filter[key];
}
}
//筛选
let resultArr = arr.filter(
(item) => {
let flag = false;
for(key in tempFilter) {
if(item[key].toString().indexOf(tempFilter[key].toString()) >= 0) {
flag = true;
} else {
flag = false;
break;
}
}
if(flag) {
return item;
}
}
);
console.log(resultArr);
边栏推荐
- GZIPOutputStream 类源码分析
- ExcelPatternTool: Excel form-database mutual import tool
- DAO开发教程【WEB3.0】
- Become a Contributor in 30 minutes | How to participate in OpenHarmony's open source contributions in multiple ways?
- 日志工厂(详细)
- To drive efficient upstream and downstream collaboration, how can cross-border B2B e-commerce platforms release the core value of the LED industry supply chain?
- 小白系统初始化配置资源失败怎么办
- 123123123123
- [pyqt5] Custom controls to achieve scaling sub-controls that maintain the aspect ratio
- Redis的内存淘汰策略和过期删除策略的区别是什么
猜你喜欢
小白系统初始化配置资源失败怎么办
Win11如何开启剪贴板自动复制?Win11开启剪贴板自动复制的方法
shell脚本专题(07):文件由cfs到bos
Redis启动时提示Creating Server TCP listening socket *:6379: bind: No error
升哲科技携全域数字化方案亮相2022全球数字经济大会
Flowable-based upp (unified process platform) running performance optimization
No need to crack, install Visual Studio 2013 Community Edition on the official website
Source code analysis of GZIPOutputStream class
Prometheus's Recording rules practice
Website construction process
随机推荐
Goldfish Brother RHCA Memoirs: CL210 manages OPENSTACK network -- network configuration options
DAO开发教程【WEB3.0】
How to record and analyze your alchemy process - use notes of the visual artifact Wandb [1]
2022,程序员应该如何找工作
GZIPOutputStream 类源码分析
小白系统初始化配置资源失败怎么办
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)
文库网站建设源码分享
cf:D. Magical Array【数学直觉 + 前缀和的和】
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
面试必问的HashCode技术内幕
选择合适的 DevOps 工具,从理解 DevOps 开始
生命周期和作用域
vtk体绘制代码报错的解决办法(代码在vtk7,8,9中都能运行),以及VTK数据集网站
力扣刷题之求两数之和
Flowable-based upp (unified process platform) running performance optimization
Industry Salon Phase II丨How to enable chemical companies to reduce costs and increase efficiency through supply chain digital business collaboration?
MySQL开发技巧——存储过程
DAO development tutorial [WEB3.0]
明日盛会|ApacheCon Asia 2022 Pulsar 技术议题一览