JavaScript tarafından Victor Talmacinschi • June 23, 2022
const obj = {
foo: '🍎',
bar: '🍌',
baz: null,
qux: undefined,
};
// nesnenin kendi numaralandırılabilir özellikleri üzerinde yineleme
for (const key of Object.keys(obj)) {
// özelliğin değeri null ise, onu sil
if (obj[key] === null) {
delete obj[key];
}
}
console.log(obj);
// { foo: '🍎', bar: '🍌' }
0
30.100
JavaScript tarafından Victor Talmacinschi • June 23, 2022
const obj = {
name: 'John',
age: null,
job: 'teacher',
city: null
};
// for döngüsü kullanın
for (let key in obj) {
if (obj[key] === null) {
delete obj[key];
}
}
// Object.keys() kullanın
const obj = {
name: 'John',
age: null,
job: 'teacher',
city: null
};
const newObj = Object.keys(obj).reduce((acc, key) => {
if (obj[key]) {
acc[key] = obj[key];
}
return acc;
}, {});
console.log(newObj); // { name: 'John', job: 'teacher' }
0
30.100
JavaScript tarafından Radu TM • June 23, 2022
const obj = {
name: '🐶',
age: undefined,
color: 'brown',
weight: undefined
};
// obj'den tüm tanımlanmamış değerleri kaldır
Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key]);
console.log(obj); // { name: '🐶', color: 'brown' }
0
11.544
JavaScript tarafından Radu TM • June 23, 2022
const obj = {
name: 'John',
age: undefined,
gender: 'male'
};
// tanımlanmamış tüm değerleri nesneden kaldırır
Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key]);
console.log(obj);
// { name: 'John', gender: 'male' }
0
11.544