如何从不需要的键数组中选择对象中的项,并使用结果创建新对象



也许这是一个奇怪的问题,也许这是存在的,还有其他方法。。。但是,我有一个初始对象,我在上面迭代键。对于一个特殊的任务,我需要有一个没有键值的Object。为了做到这一点,我用我不想要的钥匙制作了一个物体。

我制作了这个代码笔作为示例:https://codepen.io/charlene-bx/pen/qBXaqEL

const myInitialObject = {
'key#one' : 'dataofanotherObject',
'key#two' : 'dataofanotherObject',
'key#three' : 'dataofanotherObject',
'key#four' : 'dataofanotherObject',
'key#five' : 'dataofanotherObject',
'key#six' : 'dataofanotherObject',
'key#seven' : 'dataofanotherObject',
'key#eight' : 'dataofanotherObject',
'key#nine' : 'dataofanotherObject',
'key#ten' : 'dataofanotherObject'
}
const unDesiredKey = [
'one',
'four',
'six'
]
// expected output:
// {
//  'key#two' : 'dataofanotherObject',
//  'key#three' : 'dataofanotherObject',
//  'key#five' : 'dataofanotherObject',
//  'key#seven' : 'dataofanotherObject',
//  'key#eight' : 'dataofanotherObject',
//  'key#nine' : 'dataofanotherObject',
//  'key#ten' : 'dataofanotherObject'
// }

我找到了这个解决方案,但我觉得它不太可读:

const filteredObject = Object.keys(myInitialObject)
.filter(key => !unDesiredKey.map( el => !key.includes(el)).some(el => el===false))
.reduce((accumulator, value) => ({...accumulator, [value]: myInitialObject[value]}), {})

,你可以这么做

Object.entires()上使用Array.prototype.map()过滤出所需键值对的数组。最后使用Object.fromEntries()从键值对中构造一个新的对象

const myInitialObject = {
'key#one': 'dataofanotherObject',
'key#two': 'dataofanotherObject',
'key#three': 'dataofanotherObject',
'key#four': 'dataofanotherObject',
'key#five': 'dataofanotherObject',
'key#six': 'dataofanotherObject',
'key#seven': 'dataofanotherObject',
'key#eight': 'dataofanotherObject',
'key#nine': 'dataofanotherObject',
'key#ten': 'dataofanotherObject'
}
const unDesiredKey = [
'one',
'four',
'six'
]
const arr = Object.entries(myInitialObject).filter(x => !unDesiredKey.includes(x[0].split('#')[1]));
const finalObj = Object.fromEntries(arr);
console.log(finalObj)

您可以通过保留从falsyfind(x => k.includes(x))求值的对象来filter对象entries,并使用它们来创建新对象fromEntries

const myInitialObject = {
'key#one': 'dataofanotherObject',
'key#two': 'dataofanotherObject',
'key#three': 'dataofanotherObject',
'key#four': 'dataofanotherObject',
'key#five': 'dataofanotherObject',
'key#six': 'dataofanotherObject',
'key#seven': 'dataofanotherObject',
'key#eight': 'dataofanotherObject',
'key#nine': 'dataofanotherObject',
'key#ten': 'dataofanotherObject'
}
const unDesiredKey = [
'one',
'four',
'six'
];
const result = Object.fromEntries(Object.entries(myInitialObject)
.filter(([k]) => !unDesiredKey.find(x => k.includes(x)))
);
console.log(result);

如果你愿意的话,我也可以用reduce()来实现这一点。

const obj = {
'key#one': 'dataofanotherObject',
'key#two': 'dataofanotherObject',
'key#three': 'dataofanotherObject',
'key#four': 'dataofanotherObject',
'key#five': 'dataofanotherObject',
'key#six': 'dataofanotherObject',
'key#seven': 'dataofanotherObject',
'key#eight': 'dataofanotherObject',
'key#nine': 'dataofanotherObject',
'key#ten': 'dataofanotherObject'
}
const unDesiredKey = [
'one',
'four',
'six'
];
const result = Object.entries(obj).reduce((acc, [k, v]) => {
if (unDesiredKey.find(x => k.includes(x))) return acc;
acc = { ...acc, [k]: v }
return acc;
}, {});
console.log(result);

您可以过滤条目并对键进行切片以进行比较。

const
data = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' },
unDesiredKey = ['one', 'four', 'six'],
result = Object.fromEntries(Object
.entries(data)
.filter(([key]) => !unDesiredKey.includes(key.slice(key.indexOf('#') + 1)))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

所提供的方法使用reduce方法的累积对象作为最终resultomissibles的载体,作为不需要的密钥的廉价/快速查找结构(而不是例如基于数组的includesfind)。。。

const myInitialObject = {
'key#one' : 'dataofanotherObject',
'key#two' : 'dataofanotherObject',
'key#three' : 'dataofanotherObject',
'key#four' : 'dataofanotherObject',
'key#five' : 'dataofanotherObject',
'key#six' : 'dataofanotherObject',
'key#seven' : 'dataofanotherObject',
'key#eight' : 'dataofanotherObject',
'key#nine' : 'dataofanotherObject',
'key#ten' : 'dataofanotherObject',
};
const unDesiredKey = [
'one',
'four',
'six',
];
console.log(
Object
.entries(myInitialObject)
.reduce(({ omissibles, result }, [key, value]) => ({
omissibles,
result: (key.split('#')[1] in omissibles)
? result
: Object.assign(result, { [key]: value }),
}), {
omissibles: Object.fromEntries(unDesiredKey.map(key => [key, true])),
result: {},
}).result
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

基于查找的方法还通过将查找作为第二thisArg属性提供给条目filter的任务来过滤想要的条目(跳过不想要的条目),这导致比上面的代码更精简的代码。。。

const myInitialObject = {
'key#one' : 'dataofanotherObject',
'key#two' : 'dataofanotherObject',
'key#three' : 'dataofanotherObject',
'key#four' : 'dataofanotherObject',
'key#five' : 'dataofanotherObject',
'key#six' : 'dataofanotherObject',
'key#seven' : 'dataofanotherObject',
'key#eight' : 'dataofanotherObject',
'key#nine' : 'dataofanotherObject',
'key#ten' : 'dataofanotherObject',
};
const unDesiredKey = [
'one',
'four',
'six',
];
console.log(
Object.fromEntries(
Object
.entries(myInitialObject)
.filter(function getValidEntryByBoundOmissibleKeys ([key]) {
return !(key.split('#')[1] in this);
}, Object.fromEntries(unDesiredKey.map(key => [key, true])))
)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

相关内容

  • 没有找到相关文章

最新更新