js之对象数组操作——添加到数组中、删除非同对象、非同对象参数置空
场景1
arr1和arr2中,classify和workerCode相同,不往arr2中添加arr1中的对象;workerCode相同,classify不同,arr2中的classify值改为’1’;arr1对中对象的workerCode,arr2中没有,这个对象直接添加到arr2中
1
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 }, // ];
在上述代码中,我们循环遍历arr1中的每个对象,并在arr2中查找相同的对象。如果找到了一个匹配的对象,我们检查它们的classify属性是否相同,如果是,则不进行操作,如果不同,则将arr2中的对象的classify属性改为1。如果没有找到匹配的对象,我们将该对象添加到arr2数组中。
最终得到的合并后的数组为arr2。
场景2
arr1和arr2中,classify和workerCode相同,不往arr2中添加arr1中的对象;workerCode相同,classify不同,arr2中的classify值改为’1’;arr1对中对象的workerCode,arr2中没有,这个对象直接添加到arr2中
2
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 }, // ];
在上述代码中,我们循环遍历arr1
中的每个对象,并在arr2
中查找相同的对象。如果找到了一个匹配的对象,我们检查它们的classify
属性是否相同,如果是,则不进行操作,如果不同,则将arr2
中的对象的classify
属性改为1
。如果没有找到匹配的对象,我们将该对象添加到arr2
数组中。
最终得到的合并后的数组为arr2
。
场景3
arr1和arr2中,classify和workerCode相同,不往arr2中添加arr1中的对象;workerCode相同,classify不同,arr2中的classify值改为’1’;arr1对中对象的workerCode,arr2中没有,这个对象直接添加到arr2中;arr2对中对象的classify为‘1’且该对象的workerCodearr1中没有,则删除arr2中该对象
3
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); // [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "1", workerCode: 17 }, // { classify: "3", workerCode: 35 }, // ];
在上述代码中,我们首先使用循环和条件语句实现了arr1
和arr2
的合并操作,然后再使用循环和条件语句来删除arr2
中不需要的对象。具体来说,我们循环遍历arr2
中的每个对象,并在arr1
中查找是否存在与之匹配的对象,如果没有找到匹配的对象,并且该对象的classify
属性为1
,则将该对象从arr2
中删除。
最终得到的合并后的数组为arr2
。
场景4
删除arr1中比arr2中多的对象
4
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); // [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // ]
在上述代码中,我们循环遍历arr1
中的每个对象,并在arr2
中查找是否存在与之匹配的对象,如果没有找到匹配的对象,则将该对象从arr1
中删除。
需要注意的是,在删除数组元素时,我们需要将当前索引i
减1,以便于遍历到下一个元素,否则有可能会跳过某些元素。
最终得到的arr1
数组中的对象与arr2
数组中的对象一致。
场景5
arr1中比arr2中多的对象,classify值改为空字符串
5
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); // [ // { classify: "1", workerCode: 15 }, // { classify: "1", workerCode: 16 }, // { classify: "", workerCode: 17 } // ];
在上述代码中,我们循环遍历arr1
中的每个对象,并在arr2
中查找是否存在与之匹配的对象,如果没有找到匹配的对象,则将该对象的classify
属性值改为空字符串。
最终得到的arr1
数组中多余的对象的classify
属性值已经被修改为空字符串。
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/qdvuejs/10779.html