0

compare array of object with array of keys, filter array of object with array keys.

Input:

let a = ['aa'];
let b = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];

output:

b = [{bb: 2, c: 30 },{bb: 3, c: 40}];

original array should be mutate.

5 Answers 5

1

Much similiar to @SachilaRanawaka 's answer, but works without modifying the original b array:

let a = ['aa'];
let b = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];

function removeKey(obj, key) {
  let clone = Object.assign({}, obj); // <-- shallow clone
  if (key in clone) {
    delete clone[key];
  }
  return clone;
}

function removeKeys(keys, objs) {
  return objs.map(o => keys.reduce(removeKey, o));
}

console.log(removeKeys(a, b));

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

Comments

1

You could take a destructuring with getting the rest approach.

This approach does not mutate the original data.

const
    unwanted = ['aa'],
    data = [{ aa: 1, bb: 2, c: 30 }, { aa: 2, bb: 3, c: 40 }],
    result = data.map(o => unwanted.reduce((q, k) => {
        const { [k]: _, ...r } = q;
        return r;
    }, o));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

You can simply achieve this requirement with the help of Array.forEach() method.

Live Demo :

let a = ['aa'];
let b = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];

b.forEach(obj => {
  Object.keys(obj).forEach(key => {
    a.forEach(item => delete obj[item])
  });
});

console.log(b);

7 Comments

yes, but i do not want to delete that data... is it possible to filter the original array of object without delete.
@PurnimaPathak We can filter the array but here the case is bit different. We want to remove the key along with the value from the array b. As you want to mutate the array b, I think there is no harm in delete the property.
yes, but @ Rohìt Jíndal my actual scenario is that have to pass this original array (which is stored in my global state redux) from parent to child component as prop so that whenever the global state will update my prop will update too, and can get the updated prop data in child component without rendering too many times.
@PurnimaPathak Yes i Understood that, This will work in the same way as you expected. Did you tried this solution ?
yes i did, so what happening because of delete is while variable a is not containing any value or suppose removed some value from first array, then can not get back that deleted data.
|
0

It can probably be solved with less lines of code, but this was the first i could think of.

let keysToRemove = ['aa'];
let array = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];
let result = array.map((item) => {
  let filtered = Object.keys(item)
    .filter((key) => !keysToRemove.includes(key))
    .reduce((obj, key) => {
      obj[key] = item[key];
      return obj;
    }, {});
  return filtered;
});
console.log(result);

Comments

0

use the map operator and use delete to delete properties from the object

let a = ['aa'];
let b = [{ aa: 1, bb: 2, c: 30 },{ aa: 2, bb: 3, c: 40}];


const result = b.map(item => {

    Object.keys(item).forEach(key => {
       if(a.includes(key)){
          delete item[key]
       }
    })
    
    return item
  
})

console.log(result)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.