1

Is there an ES6 way to easily filter out unwanted columns? Assume we have the following data:

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

I would like to filter out the e and f values so I only have the following:

[
  { 
    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     
]

I figured it might be something similar to this, though I'm not sure how to handle the d value:

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

EDIT: Is there a way to do it without referencing the unwanted values (e or f)? In other words, only by referencing the values you want to keep (a,b,c,d)

4
  • 2
    your data is not valid, because arrays can not have properties in literal notation. Commented Jan 23, 2019 at 14:31
  • I guess you mean this d:[ {a:'blah', b:'blah', c:'blah', e:'blah', f:'blah' } ] instead of this d:[ a:'blah', b:'blah', c:'blah', e:'blah', f:'blah' ] Commented Jan 23, 2019 at 14:32
  • Fixed the typo. Thanks! Commented Jan 23, 2019 at 14:36
  • Can you do this foo.map(x=>{ return {a: x.a, b: x.b, c: x.c, d:x.d.map(xxxxxx) }}) Commented Jan 23, 2019 at 14:40

4 Answers 4

3

Use Array#map, spread syntax and 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);

Solution by referencing only wanted variables:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great, thanks! By any chance is there a way to do it without referencing the unwanted values (e or f)? In other words, only by referencing the values you want to keep (a,b,c,d)
1

You could destruture unwanted properties and take the rest parameters for properties. for nested array, you need to iterate as well.

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

Comments

0

Traverse the array using map and slice the unwanted objects. And the array you created is wrong, it never has key-value pairs. Fixed that too.

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

1 Comment

Sorry, fixed my typo. Our structures are different so please recheck the original post
0

This is also a solution but not the best one so far:

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

console.log(result);

This work around could be done in a dynamic way

Edit: I made this workaround a little more dynamic:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.