Javascript映射嵌套元素

时间:2019-01-23 14:30:13

标签: javascript

是否有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 个答案:

答案 0 :(得分:3)

使用Array#mapspread syntaxdestructuring

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);

答案 1 :(得分:1)

您可以破坏不需要的属性,并为属性获取其余参数。对于嵌套数组,还需要迭代。

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; }

答案 2 :(得分:0)

这也是一种解决方案,但不是目前为止最好的解决方案:

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);

答案 3 :(得分:0)

使用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;
}))