我的目标:从对象的键中删除空格。
例如,我有这样的记录:
const records = [
{ 'Red Blue': true, 'Orange Strawberry': true, 'Abc Xyz': true },
{ 'Blue Red': true, 'Abc Abc': true, 'Abc Xyz': true },
{ 'Yellow Green': true, 'Apple Banana': true, 'Abc Xyz': true },
]
并且必须删除每条记录的每个键的空格,如:
[
{ 'RedBlue': true, 'OrangeStrawberry': true, 'AbcXyz': true },
{ 'BlueRed': true, 'AbcAbc': true, 'AbcXyz': true },
{ 'YellowGreen': true, 'AppleBanana': true, 'AbcXyz': true },
]
问题:
我写了3个解决方案:for in
,Object.keys().forEach
和reduce
。
_
const records = [
{ "Red Blue": true, "Orange Strawberry": true, "Abc Xyz": true },
{ "Blue Red": true, "Abc Abc": true, "Abc Xyz": true },
{ "Yellow Green": true, "Apple Banana": true, "Abc Xyz": true },
];
/* 1) for...in */
console.time && console.time('solution 1');
const solution1 = records.map(record => {
const newRecord = {};
for (const key in record) {
newRecord[key.replace(/\s/g, "")] = record[key];
}
return newRecord;
});
console.timeEnd && console.timeEnd('solution 1');
/* 2) Object.keys(records).forEach */
console.time && console.time('solution 2');
const solution2 = records.map(parent => {
const newParent = {};
Object.keys(parent).forEach(key => {
newParent[key.replace(/\s/g, "")] = parent[key];
});
return newParent;
});
console.timeEnd && console.timeEnd('solution 2');
/* 3) reduce */
console.time && console.time('solution 3');
const solution3 = records.map(parent => {
return Object.keys(parent).reduce((acc, key) => ({
...acc,
[key.replace(/\s/g, "")]: parent[key],
}), {});
});
console.timeEnd && console.timeEnd('solution 3');
/* All solutions has the same result */
console.log({
solution1,
solution2,
solution3,
});
.as-console-wrapper { max-height: 100% !important; top: 0; }
更新:添加console.time
来测量每个解决方案的执行时间。
诸如“什么是更好的”之类的问题是主观的,答案通常是“只要有诀窍,最适合你的是最好的”。但是,人们普遍认为,从长远来看,将代码分成可重用的部分更加清晰。在您的特定示例中,“修改某个对象的键”和“删除空白”是两个松散相关的部分,每个部分都可以单独使用,因此对它们进行编码“更好”,例如:
function mapKeys(obj, fn) {
let res = {};
for (let [k, v] of Object.entries(obj))
res[fn(k)] = v;
return res;
}
和
let removeSpaces = x => x.replace(/\s+/g, '');
然后,为了解决手头的问题,你只需将两个部分组合在一起:
newRecords = records.map(rec => mapKeys(rec, removeSpaces))
const records = [
{ "Red Blue": true, "Orange Strawberry": true, "Abc Xyz": true, hello: 'world' },
{ "Blue Red": true, "Abc Abc": true, "Abc Xyz": true },
{ "Yellow Green": true, "Apple Banana": true, "Abc Xyz": true },
]
console.time && console.time('Execution time');
const newRecords = records.map(r => {
const rKeys = Object.keys(r)
let refObj = {}
rKeys.forEach(k => {
let tempKey = k
if (k.indexOf(' ') !== -1) tempKey = k.replace(' ', '')
refObj[tempKey] = r[k]
})
return refObj
});
console.timeEnd && console.timeEnd('Execution time');
console.log(newRecords)
const records = [
{ 'Red Blue': true, 'Orange Strawberry': true, 'Abc Xyz': true },
{ 'Blue Red': true, 'Abc Abc': true, 'Abc Xyz': true },
{ 'Yellow Green': true, 'Apple Banana': true, 'Abc Xyz': true }
];
console.time && console.time('Execution time');
for (let record of records) {
for (let key in record) {
record[key.split(' ').join('')] = record[key];
if (key.split(' ').join('') !== key) delete record[key];
}
}
console.timeEnd && console.timeEnd('Execution time');
console.log(records);
您可以使用一些正则表达式来删除空格:
const records = [
{ 'Red Blue': true, 'Orange Strawberry': true, 'Abc Xyz': true },
{ 'Blue Red': true, 'Abc Abc': true, 'Abc Xyz': true },
{ 'Yellow Green': true, 'Apple Banana': true, 'Abc Xyz': true },
]
const noSpaces = JSON.parse(JSON.stringify(records).replace(/\s(?=\w.+":)/gm, ''))
console.log(noSpaces)