当前位置:网站首页 > Vue.js开发 > 正文

js中,数组对象操作——双层遍历-for循环之splice-删除、push-添加 & 数组中添加对象 & 删除数组中对象 & 数组中对象的参数值置空

js中,数组对象操作——双层遍历-for循环之splice-删除、push-添加 & 数组中添加对象 & 删除数组中对象 & 数组中对象的参数值置空

index.js

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <script> // 场景1 // arr1和arr2中,classify和workerCode相同,不往arr2中添加arr1中的对象;workerCode相同,classify不同,arr2中的classify值改为'1';arr1对中对象的workerCode,arr2中没有,这个对象直接添加到arr2中 // let arr1 = [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "1", workerCode: 17 }, // ]; // let arr2 = [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "2", workerCode: 17 }, // { classify: "3", workerCode: 35 }, // ]; // for (let i = 0; i < arr1.length; i++) { 
      // let found = false; // for (let j = 0; j < arr2.length; j++) { 
      // if ( // arr1[i].classify === arr2[j].classify && // arr1[i].workerCode === arr2[j].workerCode // ) { 
      // found = true; // break; // } else if ( // arr1[i].workerCode === arr2[j].workerCode && // arr1[i].classify !== arr2[j].classify // ) { 
      // arr2[j].classify = "1"; // found = true; // break; // } // } // if (!found) { 
      // arr2.push(arr1[i]); // } // } // console.log(arr2); // [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "1", workerCode: 17 }, // { classify: "3", workerCode: 35 }, // ]; // 场景2 // arr1和arr2中,classify和workerCode相同,不往arr2中添加arr1中的对象;workerCode相同,classify不同,arr2中的classify值改为'1';arr1对中对象的workerCode,arr2中没有,这个对象直接添加到arr2中 // let arr1 = [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "1", workerCode: 17 }, // ]; // let arr2 = [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "2", workerCode: 17 }, // { classify: "3", workerCode: 35 }, // ]; // for (let i = 0; i < arr1.length; i++) { 
      // let found = false; // for (let j = 0; j < arr2.length; j++) { 
      // if ( // arr1[i].classify === arr2[j].classify && // arr1[i].workerCode === arr2[j].workerCode // ) { 
      // found = true; // break; // } else if ( // arr1[i].workerCode === arr2[j].workerCode && // arr1[i].classify !== arr2[j].classify // ) { 
      // arr2[j].classify = "1"; // found = true; // break; // } // } // if (!found) { 
      // arr2.push(arr1[i]); // } // } // console.log(arr2); //场景3 //arr1和arr2中,classify和workerCode相同,不往arr2中添加arr1中的对象;workerCode相同,classify不同,arr2中的classify值改为'1';arr1对中对象的workerCode,arr2中没有,这个对象直接添加到arr2中;arr2对中对象的classify为‘1’且该对象的workerCodearr1中没有,则删除arr2中该对象 // let arr1 = [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "1", workerCode: 17 }, // ]; // let arr2 = [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "2", workerCode: 17 }, // { classify: "3", workerCode: 35 }, // ]; // for (let i = 0; i < arr1.length; i++) { 
      // let found = false; // for (let j = 0; j < arr2.length; j++) { 
      // if ( // arr1[i].classify === arr2[j].classify && // arr1[i].workerCode === arr2[j].workerCode // ) { 
      // found = true; // break; // } else if ( // arr1[i].workerCode === arr2[j].workerCode && // arr1[i].classify !== arr2[j].classify // ) { 
      // arr2[j].classify = "1"; // found = true; // break; // } // } // if (!found) { 
      // arr2.push(arr1[i]); // } // } // for (let i = 0; i < arr2.length; i++) { 
      // let found = false; // for (let j = 0; j < arr1.length; j++) { 
      // if ( // arr2[i].classify === "1" && // arr2[i].workerCode === arr1[j].workerCode // ) { 
      // found = true; // break; // } // } // if (!found && arr2[i].classify === "1") { 
      // arr2.splice(i, 1); // i--; // } // } // console.log(arr2); // 场景4 // 删除arr1中比arr2中多的对象 // let arr1 = [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "1", workerCode: 17 }, // ]; // let arr2 = [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // ]; // for (let i = 0; i < arr1.length; i++) { 
      // let found = false; // for (let j = 0; j < arr2.length; j++) { 
      // if ( // arr1[i].classify === arr2[j].classify && // arr1[i].workerCode === arr2[j].workerCode // ) { 
      // found = true; // break; // } // } // if (!found) { 
      // arr1.splice(i, 1); // i--; // } // } // console.log(arr1); // 场景5 // arr1中比arr2中多的对象,classify值改为空字符串 let arr1 = [ { 
      classify: "1", workerCode: 15 }, { 
      classify: "1", workerCode: 16 }, { 
      classify: "1", workerCode: 17 }, ]; let arr2 = [ { 
      classify: "1", workerCode: 15 }, { 
      classify: "1", workerCode: 16 }, ]; for (let i = 0; i < arr1.length; i++) { 
      let found = false; for (let j = 0; j < arr2.length; j++) { 
      if ( arr1[i].classify === arr2[j].classify && arr1[i].workerCode === arr2[j].workerCode ) { 
      found = true; break; } } if (!found) { 
      arr1[i].classify = ""; } } console.log(arr1); </script> </body> </html> 
到此这篇js中,数组对象操作——双层遍历-for循环之splice-删除、push-添加 & 数组中添加对象 & 删除数组中对象 & 数组中对象的参数值置空的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • vue2中,html2canvas组件的使用——实现截图并保存到本地2024-11-27 21:00:06
  • vue中使用图像编辑器tui-image-editor(一)2024-11-27 21:00:06
  • vue2.0中监听watch的三种写法2024-11-27 21:00:06
  • vue中this.$route.query和this.$route.params & query传参和params传参的使用和区别2024-11-27 21:00:06
  • vue中,动态绑定样式——动态绑定style写法 & 动态class写法2024-11-27 21:00:06
  • vue混入实例2024-11-27 21:00:06
  • vue2中,data为什么是函数2024-11-27 21:00:06
  • vue3中,js-cookie的使用 & token之获取token-getToken()、存储token-setToken()、移除token-removeToken()2024-11-27 21:00:06
  • vue3中,当前页中封装tab切换模块2024-11-27 21:00:06
  • vue3中,封装el-tree树形控件组件,用于组织架构——defineEmits、defineProps& vue3之computed计算属性、watch和nextTick监听的写法 & `${写法2024-11-27 21:00:06
  • 全屏图片