zoukankan      html  css  js  c++  java
  • js 操作数组(过滤对应数据)

    一、过滤掉相应数据(带有key)

    var fileList = {
            "85968439868a92": [{name: 'food.jpeg'}, {name: 'ood.jpeg'}],
            "15968439111111": [{name: 'd.jpeg'}, {name: 'doe.jpeg'}]
        };
        function demo() {
            Object.keys(fileList).forEach(function (k) {
                console.log(k)
                console.log(fileList[k])
                var f = fileList[k].filter(gl)
                console.log(f)
                fileList[k] = f
            })
            console.log(fileList)
        }
        function gl(ele) {
            console.log(1)
            console.log(ele)
            if (ele.name !== 'food.jpeg'){
                return ele
            }
        }
        demo()

    二、过滤掉含有某些id 的数据

      比如:如何去除数组a中 id=15和id=3 的对象

    var a = [{ id: 15 }, { id: -1 }, { id: 0 }, { id: 3 }, { id: 12.2 }];
    var b = [15, 3];
    var c = a.filter(item => { return !b.includes(item.id); });
    
    console.log(c)

    三、过滤多维数组的相应数据

    function f () {
        var treeData = [
          {a:'1',sub:[{aaa:'ggg',is_show: 0}]},
          {a:'hhh',sub:[{aaa:'rrr',is_show: 1}]},
        ]
        var a = JSON.parse(JSON.stringify(treeData)).map(item => {
          console.log(item)
          if (item.sub) {
            item.sub = item.sub.filter(sub => sub.is_show == 1)
          }
          return item;
        }).filter(item => item.sub && item.sub.length);
        console.log(a)
      }

    四、筛选出对象中有值的数据

    var query= {
          a1: "nn",
          a2: "",
          a3: "bb"
        }
    
        var a = Object.keys( query ).reduce( ( result, key ) => {
          if ( query[ key ] !== "" ) {
            result[ key ] = query[ key ];
          }
          return result;
        }, {} );
        console.log(a)
  • 相关阅读:
    34、JS/AJAX
    33、mybatis(二)
    32、mybatis
    31、springmvc(注解)
    30、springmvc
    29、Oralce(五)
    Spring学习之路-SpringBoot简单入门
    Spring学习之路-从放弃到入门
    心情日记
    Spring学习之路-从入门到放弃
  • 原文地址:https://www.cnblogs.com/guoxianglei/p/7229102.html
Copyright © 2011-2022 走看看