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-添加 & 数组中添加对象 & 删除数组中对象 & 数组中对象的参数值置空的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/qdvuejs/10716.html