代码之家  ›  专栏  ›  技术社区  ›  ekjcfn3902039

javascript映射嵌套元素

  •  1
  • ekjcfn3902039  · 技术社区  · 7 年前

    是否有ES6方法可以轻松过滤掉不需要的列?假设我们有以下数据:

    const foo = [
      { 
        a:'blah', 
        b:'blah', 
        c:'blah', 
        d:[
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          //... N number of objects     
        ],
      },
      { 
        a:'blah', 
        b:'blah', 
        c:'blah', 
        d:[
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          //... N number of objects     
        ],
      },
      //... N number of objects     
    ];
    

    我想过滤掉E和F值,所以我只有以下内容:

    [
      { 
        a:'blah', 
        b:'blah', 
        c:'blah', 
        d:[
          { a:'blah', b:'blah', c:'blah' },
          { a:'blah', b:'blah', c:'blah' },
          { a:'blah', b:'blah', c:'blah' },
          //... N number of objects     
        ],
      },
      { 
        a:'blah', 
        b:'blah', 
        c:'blah', 
        d:[
          { a:'blah', b:'blah', c:'blah' },
          { a:'blah', b:'blah', c:'blah' },
          { a:'blah', b:'blah', c:'blah' },
          //... N number of objects     
        ],
      },
      //... N number of objects     
    ]
    

    虽然我不知道如何处理d值,但我认为它可能与此类似:

    .map(x=>{ return {a: x.a, b: x.b, c: x.c, d:x.d}})
    

    编辑:有没有一种方法可以在不引用不需要的值(e或f)的情况下进行编辑?换言之,仅通过引用要保留的值(A、B、C、D)

    4 回复  |  直到 7 年前
        1
  •  3
  •   kockburn    7 年前

    使用 Array#map , spread syntax 和 destructuring

    const foo=[{a:'blah',b:'blah',c:'blah',d:[{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},],},{a:'blah',b:'blah',c:'blah',d:[{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},],},]
    
    const res = foo.map(({d, ...rest})=>{
      return {...rest, d: d.map(({e,f,...rest})=>({...rest}))}
    });
    
    console.log(res);

    通过只引用所需变量的解决方案:

    const foo=[{a:'blah',b:'blah',c:'blah',d:[{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},],},{a:'blah',b:'blah',c:'blah',d:[{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},{a:'blah',b:'blah',c:'blah',e:'blah',f:'blah'},],},]
    
    const res = foo.map(({d, ...rest})=>{
      return {...rest, d: d.map(({a,b,c})=>({a,b,c}))}
    });
    
    console.log(res);
        2
  •  1
  •   Nina Scholz    7 年前

    您可以消除不需要的属性,并将其余参数用于属性。对于嵌套数组,也需要迭代。

    const
        filter = ({ e, f, ...rest }) => {
            Object
                .entries(rest)
                .filter(([, v]) => Array.isArray(v))
                .forEach(([k, v]) => Object.assign(rest, { [k]: v.map(filter) }));
            return rest;
        },
        foo = [{ a: 'blah', b: 'blah', c: 'blah', d: [{ a: 'blah', b: 'blah', c: 'blah', e: 'blah', f: 'blah' }, { a: 'blah', b: 'blah', c: 'blah', e: 'blah', f: 'blah' }, { a: 'blah', b: 'blah', c: 'blah', e: 'blah', f: 'blah' }] }, { a: 'blah', b: 'blah', c: 'blah', d: [{ a: 'blah', b: 'blah', c: 'blah', e: 'blah', f: 'blah' }, { a: 'blah', b: 'blah', c: 'blah', e: 'blah', f: 'blah' }, { a: 'blah', b: 'blah', c: 'blah', e: 'blah', f: 'blah' }] }];
    
    console.log(foo.map(filter));
    .as-console-wrapper { max-height: 100% !important; top: 0; }
        3
  •  0
  •   ellipsis    7 年前

    使用map遍历数组并对不需要的对象进行切片。您创建的数组是错误的,它从来没有键值对。修正了这一点。

    const foo = [
      { 
        a:'blah', 
        b:'blah', 
        c:'blah', 
        d:[
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          //... N number of objects     
        ],
      },
      { 
        a:'blah', 
        b:'blah', 
        c:'blah', 
        d:[
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          { a:'blah', b:'blah', c:'blah', e:'blah', f:'blah'},
          //... N number of objects     
        ],
      },
      //... N number of objects     
    ];
    console.log(foo.map((e)=>{
    
    e.d.forEach((x)=>{delete x.e; delete x.f})
    return e;
    }))
        4
  •  0
  •   Osakr    7 年前

    这也是一个解决方案,但不是迄今为止最好的:

    var result = foo.map( e => {
          delete e.d[0].e;
          delete e.d[0].f;
          return e;
    });
    
    console.log(result);
    

    这项工作可以用一种动态的方式来完成。

    编辑:我让这个解决方案更具活力:

    var unwantedIndex = [
      'e', 'f'
    ];
    
    var r = foo.map( e => {
        for (let data of e.d) {
            for (let indx of unwantedIndex) {
                if (data[indx]) {
                    delete data[indx];
                }
            }
        }
    
       return e;
    });
    
    console.log(r);