当前位置:网站首页>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);
边栏推荐
- 日志工厂(详细)
- 通配符 SSL/TLS 证书
- 【LeetCode】Day109-the longest palindrome string
- PanGu-Coder:函数级的代码生成模型
- GZIPOutputStream 类源码分析
- 百度无人驾驶商业化已“上路”
- When compiling a program with boost library with VS2013, it prompts fatal error C1001: An internal error occurred in the compiler
- #yyds dry goods inventory# Interview must brush TOP101: the last k nodes in the linked list
- ssh & scp
- ExcelPatternTool: Excel form-database mutual import tool
猜你喜欢
随机推荐
【webrtc】sigslot : 继承has_slot 及相关流程和逻辑
When installing the GBase 8c database, the error message "Resource: gbase8c already in use" is displayed. How to deal with this?
【神经网络】一文带你轻松解析神经网络(附实例恶搞女友)
DAO development tutorial [WEB3.0]
力扣刷题之合并两个有序数组
modbus bus module DAM-8082
vtk体绘制代码报错的解决办法(代码在vtk7,8,9中都能运行),以及VTK数据集网站
10 个 PHP 代码安全漏洞扫描程序
Heavy cover special | build the first line of defense, cloud firewall offensive and defensive drills best practices
DAO开发教程【WEB3.0】
[Server data recovery] Data recovery case of offline multiple disks in mdisk group of server Raid5 array
#yyds dry goods inventory# Interview must brush TOP101: the last k nodes in the linked list
随时随地写代码--基于Code-server部署自己的云开发环境
明尼苏达大学团队结合高通量实验与机器学习,实现有效可预测的特定位点重组过程,可调节基因编辑速度
odoo 编码规范(编程规范、编码指南)
即时通讯开发移动端弱网络优化方法总结
Screen: GFF, OGS, Oncell, Incell of full lamination process
生命周期和作用域
【周赛复盘】LeetCode第304场单周赛
What are the application advantages of SaaS management system?How to efficiently improve the digital and intelligent development level of food manufacturing industry?








