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
5 Answers
Reset to default 6simply 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 froma
.
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
usingObject.keys
- Use
reduce()
on that to build an object whose values will be froma
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
版权声明:本文标题:javascript - How delete properties of one object that are not in another object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741429474a2378269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论