B2B网络软件

标题: AIWORK软件数组高级示例 [打印本页]

作者: YYPOST群发软件    时间: 9 小时前
标题: AIWORK软件数组高级示例
AIWORK软件数组高级示例
AIWORK软件数组高级示例 B2B网络软件

AIWORK软件数组高级示例 B2B网络软件


  1. /**
  2. *🍎交流QQ群711841924群一,苹果内测群,528816639
  3. * AIWROK 平台数组方法高级应用示例
  4. * 适用于 Android 平台的 JavaScript 引擎 Rhino
  5. * 专注于数组方法的复杂应用
  6. */

  7. print.log(logWindow.show());
  8. print.log(logWindow.clear());
  9. print.log(logWindow.setHeight(2500));
  10. print.log(logWindow.setWidth(1500));
  11. print.log(logWindow.setNoClickModel());

  12. print.log(logWindow.setColor('red'));

  13. // 数组创建和初始化
  14. var arr1 = [1, 2, 3, 4, 5];
  15. var arr2 = new Array(5); // 创建长度为5的空数组
  16. var arr3 = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
  17. var arr4 = [
  18.     {id: 1, name: '张三', age: 25, city: '北京'},
  19.     {id: 2, name: '李四', age: 30, city: '上海'},
  20.     {id: 3, name: '王五', age: 22, city: '广州'},
  21.     {id: 4, name: '赵六', age: 28, city: '深圳'},
  22.     {id: 5, name: '孙七', age: 35, city: '杭州'}
  23. ];

  24. // 1. push() 和 pop() 方法 - 添加和移除数组末尾元素
  25. printl("=== push() 和 pop() 方法 ===");
  26. var fruits = ['apple', 'banana'];
  27. printl("初始数组: " + JSON.stringify(fruits));

  28. fruits.push('orange'); // 添加到末尾
  29. printl("push后: " + JSON.stringify(fruits));

  30. var removed = fruits.pop(); // 移除末尾元素
  31. printl("pop返回值: " + removed + ", 数组变为: " + JSON.stringify(fruits));

  32. // 2. unshift() 和 shift() 方法 - 添加和移除数组开头元素
  33. printl("\n=== unshift() 和 shift() 方法 ===");
  34. var numbers = [2, 3, 4];
  35. printl("初始数组: " + JSON.stringify(numbers));

  36. numbers.unshift(1); // 添加到开头
  37. printl("unshift后: " + JSON.stringify(numbers));

  38. var shifted = numbers.shift(); // 移除开头元素
  39. printl("shift返回值: " + shifted + ", 数组变为: " + JSON.stringify(numbers));

  40. // 3. slice() 方法 - 提取数组片段
  41. printl("\n=== slice() 方法 ===");
  42. var colors = ['red', 'green', 'blue', 'yellow', 'purple'];
  43. printl("原数组: " + JSON.stringify(colors));
  44. printl("slice(1, 3): " + JSON.stringify(colors.slice(1, 3))); // 提取索引1到2的元素
  45. printl("slice(2): " + JSON.stringify(colors.slice(2))); // 从索引2开始到结束

  46. // 4. splice() 方法 - 添加、删除或替换数组元素
  47. printl("\n=== splice() 方法 ===");
  48. var months = ['Jan', 'March', 'April', 'June'];
  49. printl("原数组: " + JSON.stringify(months));

  50. // 在索引1处删除0个元素,然后添加'Feb'
  51. months.splice(1, 0, 'Feb');
  52. printl("splice(1, 0, 'Feb'): " + JSON.stringify(months));

  53. // 从索引4开始删除1个元素,然后添加'May'
  54. months.splice(4, 1, 'May');
  55. printl("splice(4, 1, 'May'): " + JSON.stringify(months));

  56. // 5. concat() 方法 - 连接数组
  57. printl("\n=== concat() 方法 ===");
  58. var arrA = [1, 2, 3];
  59. var arrB = [4, 5, 6];
  60. var arrC = [7, 8];
  61. var concatenated = arrA.concat(arrB, arrC, [9, 10]); // 可以连接多个数组
  62. printl("concat结果: " + JSON.stringify(concatenated));

  63. // 6. forEach() 方法 - 遍历数组
  64. printl("\n=== forEach() 方法 ===");
  65. var products = [
  66.     {name: '笔记本电脑', price: 5000},
  67.     {name: '手机', price: 3000},
  68.     {name: '平板', price: 2000}
  69. ];

  70. products.forEach(function(product, index) {
  71.     printl((index + 1) + ". " + product.name + ": ¥" + product.price);
  72. });

  73. // 7. map() 方法 - 创建新数组
  74. printl("\n=== map() 方法 ===");
  75. var prices = products.map(function(product) {
  76.     return product.price;
  77. });
  78. printl("价格数组: " + JSON.stringify(prices));

  79. // 计算涨价后价格(涨价10%)
  80. var increasedPrices = products.map(function(product) {
  81.     return {
  82.         name: product.name,
  83.         originalPrice: product.price,
  84.         increasedPrice: product.price * 1.1
  85.     };
  86. });
  87. printl("涨价后数据: " + JSON.stringify(increasedPrices));

  88. // 8. filter() 方法 - 过滤数组元素
  89. printl("\n=== filter() 方法 ===");
  90. var expensiveProducts = products.filter(function(product) {
  91.     return product.price > 2500;
  92. });
  93. printl("高价商品: " + JSON.stringify(expensiveProducts));

  94. // 9. reduce() 方法 - 归约操作
  95. printl("\n=== reduce() 方法 ===");
  96. var totalCost = products.reduce(function(sum, product) {
  97.     return sum + product.price;
  98. }, 0);
  99. printl("总价格: ¥" + totalCost);

  100. // 10. sort() 方法 - 排序
  101. printl("\n=== sort() 方法 ===");
  102. var sortedByPrice = products.slice(); // 创建副本
  103. sortedByPrice.sort(function(a, b) {
  104.     return a.price - b.price; // 升序排列
  105. });
  106. printl("按价格升序: " + JSON.stringify(sortedByPrice));

  107. // 按姓名排序
  108. var sortedByName = arr4.slice();
  109. sortedByName.sort(function(a, b) {
  110.     var nameA = a.name.toUpperCase();
  111.     var nameB = b.name.toUpperCase();
  112.     if (nameA < nameB) return -1;
  113.     if (nameA > nameB) return 1;
  114.     return 0;
  115. });
  116. printl("按姓名排序: " + JSON.stringify(sortedByName));

  117. // 11. find() 和 findIndex() 方法
  118. printl("\n=== find() 和 findIndex() 方法 ===");
  119. var personOver30 = arr4.find(function(person) {
  120.     return person.age > 30;
  121. });
  122. printl("年龄大于30的人: " + JSON.stringify(personOver30));

  123. var indexPerson = arr4.findIndex(function(person) {
  124.     return person.city === '广州';
  125. });
  126. printl("居住在广州的人的索引: " + indexPerson);

  127. // 12. some() 和 every() 方法
  128. printl("\n=== some() 和 every() 方法 ===");
  129. var hasYoungPerson = arr4.some(function(person) {
  130.     return person.age < 25;
  131. });
  132. printl("是否有年龄小于25的人: " + hasYoungPerson);

  133. var allAdults = arr4.every(function(person) {
  134.     return person.age >= 18;
  135. });
  136. printl("所有人是否都成年: " + allAdults);

  137. // 13. 数组合并高级应用 - 复杂数据处理示例
  138. printl("\n=== 数组合并高级应用 ===");

  139. // 假设我们有一个订单系统,需要处理用户订单数据
  140. var orders = [
  141.     {userId: 1, productId: 101, quantity: 2, price: 50},
  142.     {userId: 1, productId: 102, quantity: 1, price: 30},
  143.     {userId: 2, productId: 101, quantity: 3, price: 50},
  144.     {userId: 2, productId: 103, quantity: 1, price: 80},
  145.     {userId: 3, productId: 102, quantity: 4, price: 30}
  146. ];

  147. var users = [
  148.     {id: 1, name: 'Alice', email: 'alice@example.com'},
  149.     {id: 2, name: 'Bob', email: 'bob@example.com'},
  150.     {id: 3, name: 'Charlie', email: 'charlie@example.com'}
  151. ];

  152. // 计算每个用户的总消费额
  153. var userSpending = users.map(function(user) {
  154.     var userOrders = orders.filter(function(order) {
  155.         return order.userId === user.id;
  156.     });
  157.    
  158.     var totalSpent = userOrders.reduce(function(total, order) {
  159.         return total + (order.quantity * order.price);
  160.     }, 0);
  161.    
  162.     return {
  163.         userId: user.id,
  164.         userName: user.name,
  165.         totalSpent: totalSpent,
  166.         orderCount: userOrders.length
  167.     };
  168. });

  169. printl("用户消费统计: " + JSON.stringify(userSpending));

  170. // 按消费金额排序
  171. userSpending.sort(function(a, b) {
  172.     return b.totalSpent - a.totalSpent;
  173. });
  174. printl("按消费金额排序: " + JSON.stringify(userSpending));

  175. // 找出消费最高的用户
  176. var topConsumer = userSpending[0];
  177. printl("消费最高的用户: " + JSON.stringify(topConsumer));

  178. // 14. 多维数组操作
  179. printl("\n=== 多维数组操作 ===");
  180. var matrix = [
  181.     [1, 2, 3],
  182.     [4, 5, 6],
  183.     [7, 8, 9]
  184. ];

  185. // 将二维数组扁平化
  186. var flatArray = matrix.reduce(function(acc, row) {
  187.     return acc.concat(row);
  188. }, []);
  189. printl("扁平化矩阵: " + JSON.stringify(flatArray));

  190. // 计算矩阵每行的和
  191. var rowSums = matrix.map(function(row) {
  192.     return row.reduce(function(sum, num) {
  193.         return sum + num;
  194.     }, 0);
  195. });
  196. printl("每行的和: " + JSON.stringify(rowSums));

  197. // 15. 数组去重
  198. printl("\n=== 数组去重 ===");
  199. var duplicates = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7];
  200. var unique = duplicates.filter(function(item, index, array) {
  201.     return array.indexOf(item) === index;
  202. });
  203. printl("去重前: " + JSON.stringify(duplicates));
  204. printl("去重后: " + JSON.stringify(unique));

  205. // 对象数组去重示例
  206. var people = [
  207.     {id: 1, name: '张三', age: 25},
  208.     {id: 2, name: '李四', age: 30},
  209.     {id: 1, name: '张三', age: 25}, // 重复
  210.     {id: 3, name: '王五', age: 28},
  211.     {id: 2, name: '李四', age: 30}  // 重复
  212. ];

  213. var uniquePeople = people.filter(function(person, index, array) {
  214.     return array.findIndex(function(p) {
  215.         return p.id === person.id;
  216.     }) === index;
  217. });
  218. printl("对象数组去重: " + JSON.stringify(uniquePeople));

  219. // 16. 综合示例:学生成绩分析
  220. printl("\n=== 学生成绩分析综合示例 ===");

  221. var students = [
  222.     {name: '小明', scores: [85, 92, 78, 96, 88]},
  223.     {name: '小红', scores: [95, 87, 92, 89, 94]},
  224.     {name: '小刚', scores: [78, 82, 85, 79, 83]},
  225.     {name: '小美', scores: [90, 94, 88, 92, 96]}
  226. ];

  227. // 计算每个学生的平均成绩
  228. var studentAverages = students.map(function(student) {
  229.     var average = student.scores.reduce(function(sum, score) {
  230.         return sum + score;
  231.     }, 0) / student.scores.length;
  232.    
  233.     return {
  234.         name: student.name,
  235.         average: Math.round(average * 100) / 100, // 保留两位小数
  236.         highestScore: Math.max.apply(Math, student.scores),
  237.         lowestScore: Math.min.apply(Math, student.scores)
  238.     };
  239. });

  240. printl("学生平均成绩: " + JSON.stringify(studentAverages));

  241. // 找出平均分最高的学生
  242. var topStudent = studentAverages.reduce(function(best, current) {
  243.     return current.average > best.average ? current : best;
  244. });

  245. printl("最高平均分学生: " + JSON.stringify(topStudent));

  246. // 计算班级整体平均分
  247. var classAverage = studentAverages.reduce(function(sum, student) {
  248.     return sum + student.average;
  249. }, 0) / studentAverages.length;

  250. printl("班级平均分: " + Math.round(classAverage * 100) / 100);

  251. // 统计优秀学生(平均分>=90)
  252. var excellentStudents = studentAverages.filter(function(student) {
  253.     return student.average >= 90;
  254. });

  255. printl("优秀学生人数: " + excellentStudents.length + "/" + studentAverages.length);
  256. printl("优秀学生名单: " + JSON.stringify(excellentStudents));

  257. printl("✅ 数组方法高级应用示例执行完毕");
复制代码







欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) Powered by Discuz! X3.2