Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Javascript JSON.stringify replacer return value problem?

I tried to write some javascript code that multiplies with number salaries of each person, and makes them into string with JSON.stringify.

For example, in salaries, the name and the original salary are in here like this:

let salaries = {
        james:100000,
        john:200000,
        jane:300000
    };

and I want to express like this: {james:110000,john:220000,jane:330000}

So, I wrote the code like...

let payjson = JSON.stringify(pays, function replacer(key, value){
        return value*1.1;
    }); // 1st try

and got an error, the values are changed like [object Object], NaN.

However, this code just worked for me

let payjson = JSON.stringify(pays, function replacer(key, value){
        return Number.isInteger(value)? Math.floor(value*1.1): value;
    }); // 2nd try

I wonder why the first one didn't work the way I wanted and the second just worked the way I wanted to.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

As the stringify() function documentation clearly explain, the replacer function is initially passed the object, and then each of the properties. So, you are seeing this behavior. The first time when the value comes in as object type, and the multiplication is attempted, it crashes and does not subsequently pass the properties, so the overall return of the stringify() is null. In your 2nd try code, you are checking the value and only if it is of numeric type, you are doing the math and returning a calculated value, which is used by stringify() to append to the output string.

See the documentation, and mainly this paragraph :

Initially, the replacer function is called with an empty string as key representing the object being stringified. It is then called for each property on the object or array being stringified.

about 4 years ago · Juan Pablo Isaza Denunciar

0

It looks like you want to create a copy of the original object with 10% higher salaries:

const salaries = {
    james:100000,
    john:200000,
    jane:300000
};

const res=Object.fromEntries(Object.entries(salaries).map(([k,v])=>[k,1.1*v]));

console.log(res);
// this can be strigified too:
console.log(JSON.stringify(res));
// and the input object is unchanged:
console.log(salaries);

about 4 years ago · Juan Pablo Isaza Denunciar

0

When the replacer function is first called, key is assigned an empty string. However, the function is still expected to return value, otherwise it terminates.

let salaries = {
        james:100000,
        john:200000,
        jane:300000
    };
document.write(JSON.stringify(salaries,(k,v)=>{return k==''?v:Math.floor(v*1.1);}));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda