2 回答
TA贡献1803条经验 获得超6个赞
您可以通过创建一个接受字典和匹配数组的函数来概括它,如下所示:
const dict = {
ACheckStatus: "PASS",
QVVStatus: "READY",
VVQStatus: "READY",
QTTStatus: "READY",
QBCTTStatus: "READY",
CStatus: "FAIL"
};
const matching = ['VV', 'TT', 'SS'];
function matchInput(input, matches) {
// will have matched object in the index of the match and those without a match
// at the end
const res = [];
Object.keys(input).forEach(key => {
// flag if the key wasn't matched to add it to no matches object
let matched = false;
matches.forEach((match, i) => {
if (key.indexOf(match) > 0) {
matched = true;
if (!res[i]) res[i] = {};
res[i][key] = input[key];
}
});
if (!matched) {
if (!res[matches.length]) res[matches.length] = {};
res[matches.length][key] = input[key];
}
});
return res;
}
这个想法是遍历每个键并将其插入正确的存储桶(对象)中
TA贡献1796条经验 获得超10个赞
您应该过滤对象键并仅选择您需要的键,然后使用 reduce 创建一个新对象:
const vvItems = Object.keys(dictElement) .filter(key => key.indexOf('VV')>= 1) .reduce((o, key) => { o[key] = dictElement[key]; return o; }, {})
添加回答
举报