This explanation simplifies the concept:
If the value is a string, it will return undefined. Otherwise, it will return the original value, effectively removing strings.
On the other hand,
If the value is a number, it will be returned. Otherwise, it will yield undefined, eliminating non-numbers. Only plain numbers will remain as a result.
const json = JSON.stringify(3, (key, value) => typeof(value) === "number" ? value : undefined);
console.log(json);
You could also have intended to execute
If the value is a number, it will return undefined. Otherwise, it will return the current value, excluding numbers while preserving everything else.
const person = {
id: 1,
firstName: 'Jack',
lastName: 'White',
age: 25,
};
const json = JSON.stringify(person, (key, value) => typeof(value) === "number" ? undefined : value);
console.log(json);
Or if you wanted only non-number primitives removed,
If the value is an object or a number, it will be kept. Otherwise, it will be discarded, maintaining the integrity of the data.
const person = {
id: 1,
firstName: 'Jack',
lastName: 'White',
age: 25,
};
const json = JSON.stringify(person, (key, value) => typeof value === "object" || typeof value === 'number' ? value : undefined);
console.log(json);