 | |  |  | AIWORK软件数组高级示例
- /**
- *🍎交流QQ群711841924群一,苹果内测群,528816639
- * AIWROK 平台数组方法高级应用示例
- * 适用于 Android 平台的 JavaScript 引擎 Rhino
- * 专注于数组方法的复杂应用
- */
-
- print.log(logWindow.show());
- print.log(logWindow.clear());
- print.log(logWindow.setHeight(2500));
- print.log(logWindow.setWidth(1500));
- print.log(logWindow.setNoClickModel());
- print.log(logWindow.setColor('red'));
- // 数组创建和初始化
- var arr1 = [1, 2, 3, 4, 5];
- var arr2 = new Array(5); // 创建长度为5的空数组
- var arr3 = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
- var arr4 = [
- {id: 1, name: '张三', age: 25, city: '北京'},
- {id: 2, name: '李四', age: 30, city: '上海'},
- {id: 3, name: '王五', age: 22, city: '广州'},
- {id: 4, name: '赵六', age: 28, city: '深圳'},
- {id: 5, name: '孙七', age: 35, city: '杭州'}
- ];
- // 1. push() 和 pop() 方法 - 添加和移除数组末尾元素
- printl("=== push() 和 pop() 方法 ===");
- var fruits = ['apple', 'banana'];
- printl("初始数组: " + JSON.stringify(fruits));
- fruits.push('orange'); // 添加到末尾
- printl("push后: " + JSON.stringify(fruits));
- var removed = fruits.pop(); // 移除末尾元素
- printl("pop返回值: " + removed + ", 数组变为: " + JSON.stringify(fruits));
- // 2. unshift() 和 shift() 方法 - 添加和移除数组开头元素
- printl("\n=== unshift() 和 shift() 方法 ===");
- var numbers = [2, 3, 4];
- printl("初始数组: " + JSON.stringify(numbers));
- numbers.unshift(1); // 添加到开头
- printl("unshift后: " + JSON.stringify(numbers));
- var shifted = numbers.shift(); // 移除开头元素
- printl("shift返回值: " + shifted + ", 数组变为: " + JSON.stringify(numbers));
- // 3. slice() 方法 - 提取数组片段
- printl("\n=== slice() 方法 ===");
- var colors = ['red', 'green', 'blue', 'yellow', 'purple'];
- printl("原数组: " + JSON.stringify(colors));
- printl("slice(1, 3): " + JSON.stringify(colors.slice(1, 3))); // 提取索引1到2的元素
- printl("slice(2): " + JSON.stringify(colors.slice(2))); // 从索引2开始到结束
- // 4. splice() 方法 - 添加、删除或替换数组元素
- printl("\n=== splice() 方法 ===");
- var months = ['Jan', 'March', 'April', 'June'];
- printl("原数组: " + JSON.stringify(months));
- // 在索引1处删除0个元素,然后添加'Feb'
- months.splice(1, 0, 'Feb');
- printl("splice(1, 0, 'Feb'): " + JSON.stringify(months));
- // 从索引4开始删除1个元素,然后添加'May'
- months.splice(4, 1, 'May');
- printl("splice(4, 1, 'May'): " + JSON.stringify(months));
- // 5. concat() 方法 - 连接数组
- printl("\n=== concat() 方法 ===");
- var arrA = [1, 2, 3];
- var arrB = [4, 5, 6];
- var arrC = [7, 8];
- var concatenated = arrA.concat(arrB, arrC, [9, 10]); // 可以连接多个数组
- printl("concat结果: " + JSON.stringify(concatenated));
- // 6. forEach() 方法 - 遍历数组
- printl("\n=== forEach() 方法 ===");
- var products = [
- {name: '笔记本电脑', price: 5000},
- {name: '手机', price: 3000},
- {name: '平板', price: 2000}
- ];
- products.forEach(function(product, index) {
- printl((index + 1) + ". " + product.name + ": ¥" + product.price);
- });
- // 7. map() 方法 - 创建新数组
- printl("\n=== map() 方法 ===");
- var prices = products.map(function(product) {
- return product.price;
- });
- printl("价格数组: " + JSON.stringify(prices));
- // 计算涨价后价格(涨价10%)
- var increasedPrices = products.map(function(product) {
- return {
- name: product.name,
- originalPrice: product.price,
- increasedPrice: product.price * 1.1
- };
- });
- printl("涨价后数据: " + JSON.stringify(increasedPrices));
- // 8. filter() 方法 - 过滤数组元素
- printl("\n=== filter() 方法 ===");
- var expensiveProducts = products.filter(function(product) {
- return product.price > 2500;
- });
- printl("高价商品: " + JSON.stringify(expensiveProducts));
- // 9. reduce() 方法 - 归约操作
- printl("\n=== reduce() 方法 ===");
- var totalCost = products.reduce(function(sum, product) {
- return sum + product.price;
- }, 0);
- printl("总价格: ¥" + totalCost);
- // 10. sort() 方法 - 排序
- printl("\n=== sort() 方法 ===");
- var sortedByPrice = products.slice(); // 创建副本
- sortedByPrice.sort(function(a, b) {
- return a.price - b.price; // 升序排列
- });
- printl("按价格升序: " + JSON.stringify(sortedByPrice));
- // 按姓名排序
- var sortedByName = arr4.slice();
- sortedByName.sort(function(a, b) {
- var nameA = a.name.toUpperCase();
- var nameB = b.name.toUpperCase();
- if (nameA < nameB) return -1;
- if (nameA > nameB) return 1;
- return 0;
- });
- printl("按姓名排序: " + JSON.stringify(sortedByName));
- // 11. find() 和 findIndex() 方法
- printl("\n=== find() 和 findIndex() 方法 ===");
- var personOver30 = arr4.find(function(person) {
- return person.age > 30;
- });
- printl("年龄大于30的人: " + JSON.stringify(personOver30));
- var indexPerson = arr4.findIndex(function(person) {
- return person.city === '广州';
- });
- printl("居住在广州的人的索引: " + indexPerson);
- // 12. some() 和 every() 方法
- printl("\n=== some() 和 every() 方法 ===");
- var hasYoungPerson = arr4.some(function(person) {
- return person.age < 25;
- });
- printl("是否有年龄小于25的人: " + hasYoungPerson);
- var allAdults = arr4.every(function(person) {
- return person.age >= 18;
- });
- printl("所有人是否都成年: " + allAdults);
- // 13. 数组合并高级应用 - 复杂数据处理示例
- printl("\n=== 数组合并高级应用 ===");
- // 假设我们有一个订单系统,需要处理用户订单数据
- var orders = [
- {userId: 1, productId: 101, quantity: 2, price: 50},
- {userId: 1, productId: 102, quantity: 1, price: 30},
- {userId: 2, productId: 101, quantity: 3, price: 50},
- {userId: 2, productId: 103, quantity: 1, price: 80},
- {userId: 3, productId: 102, quantity: 4, price: 30}
- ];
- var users = [
- {id: 1, name: 'Alice', email: 'alice@example.com'},
- {id: 2, name: 'Bob', email: 'bob@example.com'},
- {id: 3, name: 'Charlie', email: 'charlie@example.com'}
- ];
- // 计算每个用户的总消费额
- var userSpending = users.map(function(user) {
- var userOrders = orders.filter(function(order) {
- return order.userId === user.id;
- });
-
- var totalSpent = userOrders.reduce(function(total, order) {
- return total + (order.quantity * order.price);
- }, 0);
-
- return {
- userId: user.id,
- userName: user.name,
- totalSpent: totalSpent,
- orderCount: userOrders.length
- };
- });
- printl("用户消费统计: " + JSON.stringify(userSpending));
- // 按消费金额排序
- userSpending.sort(function(a, b) {
- return b.totalSpent - a.totalSpent;
- });
- printl("按消费金额排序: " + JSON.stringify(userSpending));
- // 找出消费最高的用户
- var topConsumer = userSpending[0];
- printl("消费最高的用户: " + JSON.stringify(topConsumer));
- // 14. 多维数组操作
- printl("\n=== 多维数组操作 ===");
- var matrix = [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]
- ];
- // 将二维数组扁平化
- var flatArray = matrix.reduce(function(acc, row) {
- return acc.concat(row);
- }, []);
- printl("扁平化矩阵: " + JSON.stringify(flatArray));
- // 计算矩阵每行的和
- var rowSums = matrix.map(function(row) {
- return row.reduce(function(sum, num) {
- return sum + num;
- }, 0);
- });
- printl("每行的和: " + JSON.stringify(rowSums));
- // 15. 数组去重
- printl("\n=== 数组去重 ===");
- var duplicates = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7];
- var unique = duplicates.filter(function(item, index, array) {
- return array.indexOf(item) === index;
- });
- printl("去重前: " + JSON.stringify(duplicates));
- printl("去重后: " + JSON.stringify(unique));
- // 对象数组去重示例
- var people = [
- {id: 1, name: '张三', age: 25},
- {id: 2, name: '李四', age: 30},
- {id: 1, name: '张三', age: 25}, // 重复
- {id: 3, name: '王五', age: 28},
- {id: 2, name: '李四', age: 30} // 重复
- ];
- var uniquePeople = people.filter(function(person, index, array) {
- return array.findIndex(function(p) {
- return p.id === person.id;
- }) === index;
- });
- printl("对象数组去重: " + JSON.stringify(uniquePeople));
- // 16. 综合示例:学生成绩分析
- printl("\n=== 学生成绩分析综合示例 ===");
- var students = [
- {name: '小明', scores: [85, 92, 78, 96, 88]},
- {name: '小红', scores: [95, 87, 92, 89, 94]},
- {name: '小刚', scores: [78, 82, 85, 79, 83]},
- {name: '小美', scores: [90, 94, 88, 92, 96]}
- ];
- // 计算每个学生的平均成绩
- var studentAverages = students.map(function(student) {
- var average = student.scores.reduce(function(sum, score) {
- return sum + score;
- }, 0) / student.scores.length;
-
- return {
- name: student.name,
- average: Math.round(average * 100) / 100, // 保留两位小数
- highestScore: Math.max.apply(Math, student.scores),
- lowestScore: Math.min.apply(Math, student.scores)
- };
- });
- printl("学生平均成绩: " + JSON.stringify(studentAverages));
- // 找出平均分最高的学生
- var topStudent = studentAverages.reduce(function(best, current) {
- return current.average > best.average ? current : best;
- });
- printl("最高平均分学生: " + JSON.stringify(topStudent));
- // 计算班级整体平均分
- var classAverage = studentAverages.reduce(function(sum, student) {
- return sum + student.average;
- }, 0) / studentAverages.length;
- printl("班级平均分: " + Math.round(classAverage * 100) / 100);
- // 统计优秀学生(平均分>=90)
- var excellentStudents = studentAverages.filter(function(student) {
- return student.average >= 90;
- });
- printl("优秀学生人数: " + excellentStudents.length + "/" + studentAverages.length);
- printl("优秀学生名单: " + JSON.stringify(excellentStudents));
- printl("✅ 数组方法高级应用示例执行完毕");
复制代码
| |  | |  |
|