javascript 中具有深层嵌套对象的数组的交集
Intersection of an array with a deeply nested object in javascript
我想包含数组中存在的对象的所有键并丢弃其余键,
例如,如果我有一个数组,
const arr = ['apple', 'milk', 'bread', 'coke'];
和一个对象,
const dummy = {
apple: {
category: 'fruit'
},
banana: {
category: 'fruit'
},
potato: {
category: 'vegetable'
},
dairy: {
milk: {
type: 'A2'
}
},
bakery: {
bread: {
type: 'brown'
}
},
beverage: {
cold_drink: {
coke: {
type: 'diet'
},
beer: {
}
}
}
}
我希望我的结果对象仅包含来自 arr
的键,无论是直接键还是深层嵌套。所以对于上述情况,我的结果对象看起来像,
{
apple: {
category: 'fruit'
},
dairy: {
milk: {
type: 'A2'
}
},
bakery: {
bread: {
type: 'brown'
}
},
beverage: {
cold_drink: {
coke: {
type: 'diet'
},
beer: {}
}
}
}
我正在尝试通过 递归 解决此问题,但无法正确获得输出。下面是我的代码,你能帮我看看我哪里出错了吗?
function fetchResultantObject(object, key, result) {
if(typeof object !== 'object')
return null;
for(let objKey in object) {
if(key.indexOf(objKey) > -1) {
result[objKey] = object[objKey];
} else {
result[objKey] = fetchValueByKey(object[objKey], key, result);
}
}
return result;
}
console.log(fetchResultantObject(dummy, arr, {}));
您可以过滤条目并检查(嵌套的)对象是否包含想要的键。
const
hasKey = object => object
&& typeof object === 'object'
&& (keys.some(k => k in object) || Object.values(object).some(hasKey)),
keys = ['apple', 'milk', 'bread', 'coke'],
data = { apple: { category: 'fruit' }, banana: { category: 'fruit' }, potato: { category: 'vegetable' }, dairy: { milk: { type: 'A2' } }, bakery: { bread: { type: 'brown' } }, beverage: { cold_drink: { coke: { type: 'diet' } } } },
result = Object.fromEntries(Object
.entries(data)
.filter(([k, v]) => hasKey({ [k]: v }))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
我想包含数组中存在的对象的所有键并丢弃其余键, 例如,如果我有一个数组,
const arr = ['apple', 'milk', 'bread', 'coke'];
和一个对象,
const dummy = {
apple: {
category: 'fruit'
},
banana: {
category: 'fruit'
},
potato: {
category: 'vegetable'
},
dairy: {
milk: {
type: 'A2'
}
},
bakery: {
bread: {
type: 'brown'
}
},
beverage: {
cold_drink: {
coke: {
type: 'diet'
},
beer: {
}
}
}
}
我希望我的结果对象仅包含来自 arr
的键,无论是直接键还是深层嵌套。所以对于上述情况,我的结果对象看起来像,
{
apple: {
category: 'fruit'
},
dairy: {
milk: {
type: 'A2'
}
},
bakery: {
bread: {
type: 'brown'
}
},
beverage: {
cold_drink: {
coke: {
type: 'diet'
},
beer: {}
}
}
}
我正在尝试通过 递归 解决此问题,但无法正确获得输出。下面是我的代码,你能帮我看看我哪里出错了吗?
function fetchResultantObject(object, key, result) {
if(typeof object !== 'object')
return null;
for(let objKey in object) {
if(key.indexOf(objKey) > -1) {
result[objKey] = object[objKey];
} else {
result[objKey] = fetchValueByKey(object[objKey], key, result);
}
}
return result;
}
console.log(fetchResultantObject(dummy, arr, {}));
您可以过滤条目并检查(嵌套的)对象是否包含想要的键。
const
hasKey = object => object
&& typeof object === 'object'
&& (keys.some(k => k in object) || Object.values(object).some(hasKey)),
keys = ['apple', 'milk', 'bread', 'coke'],
data = { apple: { category: 'fruit' }, banana: { category: 'fruit' }, potato: { category: 'vegetable' }, dairy: { milk: { type: 'A2' } }, bakery: { bread: { type: 'brown' } }, beverage: { cold_drink: { coke: { type: 'diet' } } } },
result = Object.fromEntries(Object
.entries(data)
.filter(([k, v]) => hasKey({ [k]: v }))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }