微搭低代码入门08数组迭代

目录

  • [1 forEach](#1 forEach)
  • [2 map](#2 map)
  • [3 filter](#3 filter)
  • [4 reduce](#4 reduce)
  • [5 findIndex](#5 findIndex)
  • [6 some](#6 some)
  • [7 every](#7 every)
  • 应用示例:答题小程序
  • 总结

在我们使用低代码工具开发具体应用的时候,对数组的操作是最频繁的,数组提供了很多便利的方法可以迭代数组里的元素,我们本篇讲解一下具体的语法,结合答题小程序的案例做一个理解。

1 forEach

forEach方法用于遍历数组中的每一个元素,并为每个元素执行提供的回调函数。

语法:

bash 复制代码
array.forEach(callback(currentValue, index, array), thisArg);

示例:

bash 复制代码
const numbers = [1, 2, 3, 4];
numbers.forEach(function(number) {
    console.log(number);
});
// 输出: 1 2 3 4

但是有一个问题是,如果forEach里出现了await,预期的结果会不同,遇到需要异步调用的,我们通常使用for...of调用

bash 复制代码
const result = await context.callModel()

for(const item of result){
   await context.callModel()
}

2 map

map方法创建一个新数组,其结果是该数组中的每个元素调用一个提供的函数后的返回值。

语法:

bash 复制代码
const newArray = array.map(callback(currentValue, index, array), thisArg);

示例:

bash 复制代码
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(function(number) {
    return number * 2;
});
console.log(doubled);
// 输出: [2, 4, 6, 8]

通常我们会用箭头函数简化

bash 复制代码
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(number=>number*2)
console.log(doubled);
// 输出: [2, 4, 6, 8]

map使用的场景非常多,通常用在组件属性的表达式绑定上,比如我们的下拉单选组件,可以查看官方文档,查看选项这个属性要求的规则

要求是label和value,但是我们数据源通常是按照字段存储数据的,不会有label和value这种结构,这就需要我们使用map方法重新构造数据结构,通常我们是这样的

bash 复制代码
$w.query1.data.records.map(item=>({label:item.***,value:item.***})

3 filter

filter方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。

语法:

bash 复制代码
const newArray = array.filter(callback(currentValue, index, array), thisArg);

示例:

bash 复制代码
const numbers = [1, 2, 3, 4, 5];
const evens = numbers.filter(function(number) {
    return number % 2 === 0;
});
console.log(evens);
// 输出: [2, 4]

filter的应用场景也比较广泛,比如我们在背单词小程序里,在一个界面先要筛查会的和不会的单词,在下一个界面去学习不会的单词,那么我们就可以通过学习状态进行过滤,这里就可以用到filter方法

4 reduce

reduce方法对数组中的每个元素执行一个提供的函数(升序执行),将其结果汇总为单个值。

语法:

bash 复制代码
const sum = array.reduce(callback(accumulator, currentValue, index, array), initialValue);

示例:

bash 复制代码
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce(function(accumulator, currentValue) {
    return accumulator + currentValue;
}, 0);
console.log(sum);
// 输出: 10

reduce的场景在点餐或者电商小程序里有比较广泛的应用,比如我们在点餐界面,你每加入一个菜品,我们就在购物车里计算总价,这里就可以用到reduce来对总价进行求和

5 findIndex

findIndex方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。

语法:

bash 复制代码
const index = array.findIndex(callback(currentValue, index, array), thisArg);

示例:

bash 复制代码
const numbers = [1, 2, 3, 4, 5];
const firstEvenIndex = numbers.findIndex(function(number) {
    return number % 2 === 0;
});
console.log(firstEvenIndex);
// 输出: 1

findIndex主要是用在,比如我有一个比较大的数组,我要查找某个元素,这里就可以通过findIndex找到索引,找到之后你可以修改元素或者做删除的动作

6 some

some方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回true或false。

语法:

bash 复制代码
const isSomeTrue = array.some(callback(currentValue, index, array), thisArg);

示例:

bash 复制代码
const numbers = [1, 2, 3, 4, 5];
const hasEven = numbers.some(function(number) {
    return number % 2 === 0;
});
console.log(hasEven);
// 输出: true

7 every

every方法测试数组的所有元素是否都通过了指定函数的测试。它返回true或false。

语法:

bash 复制代码
const isEveryTrue = array.every(callback(currentValue, index, array), thisArg);

示例:

bash 复制代码
const numbers = [2, 4, 6, 8, 10];
const areAllEven = numbers.every(function(number) {
    return number % 2 === 0;
});
console.log(areAllEven);
// 输出: true

some和every并不常用,可以理解为这两个方法可以用来做规则校验,比如你校验存在或者全部通过的规则给用户一定的提示信息

应用示例:答题小程序

假设我们有一个简单的答题小程序,其中有一组问题和答案,我们需要对这些数据进行处理:

bash 复制代码
const questions = [
    { question: "2 + 2 = ?", answer: 4 },
    { question: "3 + 3 = ?", answer: 6 },
    { question: "5 - 2 = ?", answer: 3 },
    { question: "10 / 2 = ?", answer: 5 },
    { question: "7 * 3 = ?", answer: 22 }  // 故意写错一个答案
];

// 使用filter筛选出所有正确答案的问题
const correctAnswers = questions.filter(q => q.answer === eval(q.question.split('= ')[1]));

// 使用map生成一个新的数组,只包含问题和是否正确的标记
const results = correctAnswers.map(q => ({
    question: q.question,
    correct: q.answer === eval(q.question.split('= ')[1])
}));

// 使用some检查是否有错误的答案
const hasWrongAnswer = questions.some(q => q.answer !== eval(q.question.split('= ')[1]));

// 使用every检查是否所有答案都正确
const areAllCorrect = questions.every(q => q.answer === eval(q.question.split('= ')[1]));

// 使用reduce计算正确回答的数量
const correctCount = questions.reduce((count, q) => {
    return count + (q.answer === eval(q.question.split('= ')[1]) ? 1 : 0);
}, 0);

// 使用findIndex找到第一个错误答案的索引
const firstWrongIndex = questions.findIndex(q => q.answer !== eval(q.question.split('= ')[1]));

console.log("Correct Answers:", correctAnswers);
console.log("Results with Correctness:", results);
console.log("Has Wrong Answer:", hasWrongAnswer);
console.log("Are All Correct:", areAllCorrect);
console.log("Correct Count:", correctCount);
console.log("First Wrong Index:", firstWrongIndex);

总结

在实际开发中,尤其如果应用需要频繁处理数组中的元素的,使用这种现成的方法就可以提效不少。

相关推荐
NocoBase4 小时前
程序员和软件还有前途吗 —— 从 NocoBase 收入再翻倍谈起
低代码·ai·开源·无代码·管理工具·内部工具·无代码开发平台
盟接之桥5 小时前
制造业汽车零配件EDI软件场景方案
网络·安全·低代码·汽车·制造
小龙报7 小时前
用ChatGPT 5.5构建个人写作工作流:从大纲、初稿到风格润色的提示词链
人工智能·神经网络·低代码·自然语言处理·chatgpt·gpt-3·知识图谱
撑死胆大的1 天前
2026开发变局:国标落地后,软件开发彻底换赛道
前端·低代码·ai·大模型
API开发平台2 天前
API智能开发与治理平台v5.0发布
低代码·ai编程
oioihoii2 天前
探索React与Microi吾码的完美结合:快速搭建项目,低代码便捷开发教程
react.js·低代码·rxjava
chian-ocean2 天前
Microi吾码:从零到服装ERP:低代码打造企业级系统的实战之旅
android·低代码·rxjava
卷叶小树4 天前
低代码属性面板-Setter体系与高级配置
低代码·前端框架
葡萄城技术团队4 天前
活字格:打通 ERP 与车间执行数据,实现计划与生产协同
低代码
SL_staff4 天前
《如何用规则引擎替代if-else?JVS-Rules可视化编排比硬编码强在哪里?》
java·低代码·架构