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

js之对象数组操作——添加到数组中、删除非同对象、非同对象参数置空

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 }, // ]; 

在上述代码中,我们首先使用循环和条件语句实现了arr1arr2的合并操作,然后再使用循环和条件语句来删除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属性值已经被修改为空字符串。

到此这篇js之对象数组操作——添加到数组中、删除非同对象、非同对象参数置空的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • js中,删除arr1中比arr2中多的对象之filter、find & 数组中是否有相同对象之every、some & 删除数组中不是相同的对象 & 对象数组,去重后合并2024-12-02 11:27:09
  • js将数字转换成万、亿、万亿2024-12-02 11:27:09
  • js实现页面跳转链接的几种方式2024-12-02 11:27:09
  • vue3中,校验方法之身份证号脱敏、校验数字长度、特殊字符校验2024-12-02 11:27:09
  • vue3中,form表单校验之特殊字符校验、手机号、身份证号、百分制数字 & route和router的写法 & setup的两种用法 & rules中校验之blur和change2024-12-02 11:27:09
  • vue中,js封装方法之判断js对象类型 & 封装方法之js对象深拷贝2024-12-02 11:27:09
  • vue项目中 jsconfig.json和tsconfig.json文件配置释义 & compilerOptions配置2024-12-02 11:27:09
  • js计算两个时间戳的时间差之多少天、时、分、秒,格式0000、动态显示年月日时分秒(不足两位补0)2024-12-02 11:27:09
  • vue中,格式化时间戳转换成标准时间 & 获取随机id-uuid & random-m到n的随机数2024-12-02 11:27:09
  • vue3中,父子组件props传函数写法-传动态接口 & 特殊字符校验 & 封装公共input组件2024-12-02 11:27:09
  • 全屏图片