JavaScript Compact Object All In One
JavaScript Compact Object All In One
LeetCode 2705. Compact Object
solutions
function compactObject(obj: Obj): Obj {
let temp;
if(Array.isArray(obj)) {
obj = obj.filter(i => Boolean(i));
// obj = obj.filter(i => !!Boolean(i)); ✅
// obj = obj.filter(i => Boolean(i) !== false); ✅
temp = [];
} else {
temp = {};
}
for(let key in obj) {
if(Boolean(obj[key]) !== false) {
// if(Object.prototype.toString.call(obj[key]).includes(`object`)) { ❌ bug
if(Array.isArray(obj[key]) || Object.prototype.toString.call(obj[key]) === `[object Object]`) {
temp[key] = compactObject(obj[key]);
} else {
temp[key] = obj[key];
}
}
}
return temp;
};
https://leetcode.com/problems/compact-object/?envType=study-plan-v2&envId=30-days-of-javascript
demos
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
type Obj = Record<string, JSONValue> | Array<JSONValue>;
function compactObject(obj: Obj): Obj {
let temp;
if(Array.isArray(obj)) {
obj = obj.filter(i => Boolean(i));
temp = [];
} else {
temp = {};
}
for(let key in obj) {
if(Boolean(obj[key]) !== false) {
// if(Object.prototype.toString.call(obj[key]).includes(`object`)) { ❌ bug
if(Array.isArray(obj[key]) || Object.prototype.toString.call(obj[key]) === `[object Object]`) {
temp[key] = compactObject(obj[key]);
} else {
temp[key] = obj[key];
}
}
}
return temp;
};
// function compactObject(obj: Obj): Obj {
// let temp;
// if(Array.isArray(obj)) {
// obj = obj.filter(i => Boolean(i));
// // obj = obj.filter(i => !!Boolean(i)); ✅
// // obj = obj.filter(i => Boolean(i) !== false); ✅
// temp = [];
// } else {
// temp = {};
// }
// for(let key in obj) {
// if(Boolean(obj[key]) !== false) {
// // if(Object.prototype.toString.call(obj[key]).includes(`object`)) { ❌ bug
// if(Array.isArray(obj[key]) || Object.prototype.toString.call(obj[key]) === `[object Object]`) {
// temp[key] = compactObject(obj[key]);
// } else {
// temp[key] = obj[key];
// }
// }
// }
// return temp;
// };
// function compactObject(obj: Obj): Obj {
// let temp;
// if(Array.isArray(obj)) {
// obj = obj.filter(i => Boolean(i) !== false);
// for(let i = 0; i < obj.length; i++) {
// if(Boolean(obj[i]) !== false) {
// if(Array.isArray(obj[i])) {
// obj.splice(i, 1, compactObject(obj[i]));
// }
// } else {
// obj.splice(i, 1);
// }
// }
// } else {
// for(let key in obj) {
// if(Boolean(obj[key]) !== false) {
// if(Array.isArray(obj[key])) {
// obj[key] = compactObject(obj[key]);
// }
// } else {
// delete obj[key];
// }
// }
// }
// return obj;
// };
/*
Wrong Answer ❌
69 / 156 testcases passed
Input
obj =
{"o":11,"a":68,"m":18,"v":true,"b":false,"h":null,"r":false,"p":93,"l":true,"k":"","n":79,"f":18,"u":null,"j":null,"e":null,"z":{"d":true,"t":null,"o":21,"w":null,"m":82,"k":35,"l":26,"r":45,"b":false,"z":0,"i":null,"c":33,"p":69,"j":27,"e":53,"q":97,"h":true,"a":true,"n":true,"f":73,"u":null},"w":12,"y":0,"q":"","t":null,"s":true,"x":false,"c":true,"i":false,"g":29}
Use Testcase
Output
{"o":11,"a":68,"m":18,"v":true,"p":93,"l":true,"n":79,"f":18,"z":{"d":true,"t":null,"o":21,"w":null,"m":82,"k":35,"l":26,"r":45,"b":false,"z":0,"i":null,"c":33,"p":69,"j":27,"e":53,"q":97,"h":true,"a":true,"n":true,"f":73,"u":null},"w":12,"s":true,"c":true,"g":29}
Expected
{"a":68,"c":true,"f":18,"g":29,"l":true,"m":18,"n":79,"o":11,"p":93,"s":true,"v":true,"w":12,"z":{"a":true,"c":33,"d":true,"e":53,"f":73,"h":true,"j":27,"k":35,"l":26,"m":82,"n":true,"o":21,"p":69,"q":97,"r":45}}
*/
简化版 测试用例
// 简化版 测试用例
// 1. 对象套对象
{"a": null, "b": [false, 1], "c": {"d": null, "e": [true, 2, {"f": 3}]}}
// 2. 数组套对象/数组套数组
[null, 0, false, 1, {"a": null, "b": [true, 2, {"c": 3}]}]
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
Object.prototype.toString()
Object.prototype.toString.call(null)
'[object Null]'
Object.prototype.toString.call({})
'[object Object]'
Object.prototype.toString.call([])
'[object Array]'
typeof null
'object'
typeof {}
'object'
typeof []
'object'
Object.prototype.toString([])
'[object Object]'
const toString = Object.prototype.toString;
toString.call(new Date()); // [object Date]
toString.call(new String()); // [object String]
// Math has its Symbol.toStringTag
toString.call(Math); // [object Math]
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]
refs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18198967
未经授权禁止转载,违者必究!