admin管理员组

文章数量:1290186

Say I have two objects like the ones below.

let a = {Friday: [1, 2 3], Saturday: [2,4,2], Sunday: [1,4]}
let b = {Friday: [], Saturday: []}

I need some sort of way to delete all the key value pairs from a that are not in b, so the result would be:

{Friday: [1, 2 3], Saturday: [2,4,2]}

Say I have two objects like the ones below.

let a = {Friday: [1, 2 3], Saturday: [2,4,2], Sunday: [1,4]}
let b = {Friday: [], Saturday: []}

I need some sort of way to delete all the key value pairs from a that are not in b, so the result would be:

{Friday: [1, 2 3], Saturday: [2,4,2]}
Share Improve this question asked Jun 15, 2019 at 16:32 Oamar KanjiOamar Kanji 2,2247 gold badges28 silver badges47 bronze badges 2
  • so you want only sunday? – Nina Scholz Commented Jun 15, 2019 at 16:37
  • No, the opposite, I want Friday and Saturday – Oamar Kanji Commented Jun 15, 2019 at 16:38
Add a ment  | 

5 Answers 5

Reset to default 6

simply use a for loop and delete:

  • Iterate over all the properties in a
  • check if the property exist in b, if it is not present simply delete the property from a.

let a = {Friday: [1, 2, 3], Saturday: [2,4,2], Sunday: [1,4]};
let b = {Friday: [], Saturday: []};

for(let key in a){
  if(!(key in b))
    delete a[key];
}
console.log(a);

  • Get the keys of b using Object.keys
  • Use reduce() on that to build an object whose values will be from a

let a = {Friday: [1, 2, 3], Saturday: [2,4,2], Sunday: [1,4]};
let b = {Friday: [], Saturday: []}

let res = Object.keys(b).reduce((ac,k) => (ac[k] = a[k],ac),{});

console.log(res)

If you are confused with one liner. Below is more easy to understand version.

let res = Object.keys(b).reduce((ac,k) => {
   ac[k] = a[a];
   return ac;
},{});

You could get all keys of a, delete from this the ones of b and delete the properties of a with it.

var a = { Friday: [1, 2, 3], Saturday: [2, 4, 2], Sunday: [1, 4] },
    b = { Friday: [], Saturday: [] };

Object
    .keys(a)
    .filter(k => !(k in b))
    .forEach(Reflect.deleteProperty.bind(null, a));

console.log(a);

The easiest way to do it is with for loop and if statement

// Removes the pairs from A that are not in B
for (let key in a) if (!b[key]) delete a[key]

In case of pare two deep objects and get second one values but only with first one properties, I write this function:

const filterObjectPropertiesDeep = (objRef, objFilterd) => {
  const _objFilterd = cloneDeep(objFilterd);

  const filter = (objRef, _objFilterd) => {
    for (let key in _objFilterd) {
      if (!(key in objRef)) delete _objFilterd[key];

      if (typeof _objFilterd[key] === 'object') {
        if (objRef[key]) {
          filter(objRef[key], _objFilterd[key]);
        } else {
          _objFilterd[key] = {};
        }
      }
    }
  };

  filter(objRef, _objFilterd);

  return _objFilterd;
};

I give you an example:

const objRef = {
  props: {
    toto: 'toto',
    sub1: {
      tata: 'tata',
    },
    sub2: {
      titi: 'titi',
    },
  },
};
const objFilterd = {
  props: {
    toto: 'toto',
    sub1: {
      tutu: 'tutu',
    },
    sub2: {
      titi: 'titi',
      tyty: 'tyty',
    },
  },
};

filterObjectPropertiesDeep(objRef, objFilterd);
// => { "props": { "toto": "toto", "sub1": {}, "sub2": { "titi": "titi" } } }

本文标签: javascriptHow delete properties of one object that are not in another objectStack Overflow