Cho object chứa thuộc tính ngẫu nhiên: số, boolean, chuỗi, object. Viết hàm nhận object và chuỗi tên kiểu (number, string, object...), xóa mọi thuộc tính kiểu đó. Mặc định xóa 'number'.
Ví dụ
const obj = { name: 'Lokesh Rahul', age: 29, mother: 'Avantika Rahul', father: 'Trilok Rahul', matches: 123, average: 45.23, isFit: true, runs: { odi: 5674, test: 3456 } }; const shedData = (obj, type = 'number') => { for(const key in obj){ if(typeof obj[key] === type){ delete obj[key]; }; }; }; shedData(obj, 'string'); console.log(obj);Kết quả
Đầu ra console:
{ age: 29, matches: 123, average: 45.23, isFit: true, runs: { odi: 5674, test: 3456 } }