select 如何从不需要的键数组中获取对象中的项目,并用结果创建一个新对象
How select items in an object from an array of undesired key and create a new object with the result
也许这是一个奇怪的问题,也许存在这个问题以及其他方法...但是,我有一个初始对象,我在该对象上迭代了键。
对于一项特殊任务,我需要这个对象没有一些 key:value。
为此,我用我不想要的键创建了一个对象。
我制作了这个码笔作为样品: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)
您可以过滤条目并切分键以进行比较。
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; }
您可以 filter
对象 entries
,方法是将计算 falsy
的对象保留为 find(x => k.includes(x))
,并使用它们创建新对象 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);
提供的方法使用 reduce
方法的累积对象作为最终 result
和 omissibles
的载体作为 inexpensive/fast 查找结构(而不是例如基于数组 includes
或 find
) 对于不需要的键 ...
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
property to the entry filter
ing 任务来过滤想要的条目(跳过不需要的条目),这会导致比上面的代码更精简...
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; }
也许这是一个奇怪的问题,也许存在这个问题以及其他方法...但是,我有一个初始对象,我在该对象上迭代了键。 对于一项特殊任务,我需要这个对象没有一些 key:value。 为此,我用我不想要的键创建了一个对象。
我制作了这个码笔作为样品: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)
您可以过滤条目并切分键以进行比较。
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; }
您可以 filter
对象 entries
,方法是将计算 falsy
的对象保留为 find(x => k.includes(x))
,并使用它们创建新对象 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);
提供的方法使用 reduce
方法的累积对象作为最终 result
和 omissibles
的载体作为 inexpensive/fast 查找结构(而不是例如基于数组 includes
或 find
) 对于不需要的键 ...
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
property to the entry filter
ing 任务来过滤想要的条目(跳过不需要的条目),这会导致比上面的代码更精简...
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; }