zoukankan      html  css  js  c++  java
  • 数组间的比较过滤/JS取出两个数组相同元素或者不同元素

    取出两个数组相同元素,组成一个新的数组对象

    let data = [ 
        {name: "name一",isSelected: false,value: 111},                                                                
        {name: "name二",isSelected: false,value: 222},
        {name: "name三",isSelected: false,value: 333},                                                                                                                            
        {name: "name四",isSelected: false,value: 444}
    ];
    let arr1 = [ 
    {  isHas: true ,                                                                                
       id: 111,                                                                                                      
       title: "no image11"                                                                                                   
    },{
       isHas: false ,                                                                                
       id: 222,                                                                                                      
       title: "no image22"                                                                                                   
    },{ 
       isHas: true ,                                                                                
       id: 333,                                                                                                      
       title: "no image33"                                                                                                   
    }];
    let newArr = [];
    let isExit;
        for (let i = 0; i < arr1.length; i++) {
            for (let j = 0; j < data.length; j++) {
                if(data[j].value === arr1[i].id){
                    isExit = true;
                }
            }
            isExit  && newArr.push(arr1[i])
     
        }
        
    console.log(newArr)

    数组间的比较过滤

    let data = [ 
     {name: "name一",isSelected: false,value: 111},                                                                
     {name: "name二",isSelected: false,value: 222},                                                                                                                        
     {name: "name三",isSelected: false,value: 333},                                                                                                                        
     {name: "name四",isSelected: false,value: 444}                                                            ];
    let arr1 = [ {  isHas: true ,                                                                                
                    id: 111,                                                                                                      
                    title: "no image11"                                                                                                   
                 },{  isHas: false ,                                                                                
                    id: 222,                                                                                                      
                    title: "no image22"                                                                                                   
                 },{ isHas: true ,                                                                                
                    id: 333,                                                                                                      
                    title: "no image33"                                                                                                   
                 }];
    let result = [];
    let isExist= false;
    for(var i = 0; i < data.length; i++){
        var obj = data[i];
        var num = obj.value;
        isExist = false;
        for(var j = 0; j < arr1.length; j++){
            var aj = arr1[j];
            var n = aj.id;
            if(aj.isHas === true && n === num){
                isExist = true;
                break;
            }
        }
        isExist&&result.push(obj);
    }
    console.log('yannan',result);
  • 相关阅读:
    清除浮动的三种方式
    js控制滚动条默认在底部
    npm 基础命令
    package.json中^,~的区别
    mac 使用iTerm2快捷登录远程服务器
    iterm 分屏切换快捷键与配色设置
    git tag — 标签相关操作
    gulp iconfont
    webpack 3 升级 webpack4 个人笔记
    import * as x from 'xx' 和 import x from 'xx'
  • 原文地址:https://www.cnblogs.com/shine1234/p/14845415.html
Copyright © 2011-2022 走看看