1

Hi I need to convert the the numeric values of my object to string. But different properties has different transformation rules.

My sample object:

{
name: "Name"
sRatio: 1.45040404
otherMetric: 0.009993
}

I use JSON.stringify to convert my initial object.

let replacemet = {}
JSON.stringify(metrics[0], function (key, value) {
  //Iterate over keys
  for (let k in value) {
    if ((k !== "sRatio") ||  (k !== "name"))  {
      replacemet[k] = (100*value[k]).toFixed(2) + "%"
    } else {
      if( k === "name") {
        replacemet[k] = "yo!"+value[k]
      } else{
        replacemet[k] = value[k].toFixed(2)
      }
    }
  }
})

But my conditions are not triggered and all properties are converting on the same manner.

Daniel Chepenko
  • 2,229
  • 7
  • 30
  • 56
  • 5
    `if ((k !== "sRatio") || (k !== "name"))` will always be true. Just use `if (k == "name") {} else if (k == "sRatio") {} else {}` – Reinstate Monica Cellio Jul 25 '18 at 09:36
  • `||` should be `&&`. See https://stackoverflow.com/questions/26337003/execute-block-if-a-variable-is-not-one-of-some-specific-values – Barmar Jul 25 '18 at 09:44
  • 1
    You use `replacer` parameter in wrong way. It must **return** value, otherwise a property is not included. – hindmost Jul 25 '18 at 09:45

3 Answers3

1

The job of the replacer callback is not to fill in some global replacemet object but rather to return a new value.

I think you are looking for something along the lines of

JSON.stringify(sample, function (key, value) {
  if (key == "sRatio") {
    return value.toFixed(2);
  } else if (key == "name") {
    return "yo!"+value;
  } else if (typeof value == "number") {
    return (100*value).toFixed(2) + "%"
  } else {
    return value;
  }
})
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Try using switch block that will be really good for this. Detailed description on switch.

let replacemet = {}
JSON.stringify(metrics[0], function (key, value) {
  //Iterate over keys
  for (let k in value) {       
    switch(k) {
    case "name":
        replacemet[k] = "yo!"+value[k];
        break;
    case "sRatio":
        replacemet[k] = value[k].toFixed(2);
        break;
    default:
        replacemet[k] = value[k].toFixed(2);
    }
  }
})
Binit Ghetiya
  • 1,919
  • 2
  • 21
  • 31
0

Hope to help you . I add when dynamic property

         metrics =
            [
            {
            name: "Name",
            sRatio: 1.45040404,
            otherMetric:0.009993
            },
            {
            name: "Name1",
            sRatio: 2.45040404,
            otherMetric: 1.009993
            }
             ]
           ;
            let source = JSON.stringify(metrics);

            
           
            let arrJson  = new Array();
            //arrJson = {};
            metrics.forEach(function(value){
                let replacemet = {};
            
                for(var k in value) {
                
                 
                    if( k.toString().trim()  == "name") {
                        replacemet[k] =    "yo!"+value[k] ;
                   
                      } 
                     else 
                    if ( (  k.toString().trim() !== "sRatio") &&  ( k.toString().trim()  !== "name"))  {
                      replacemet[k] =  (100* value[k] ).toFixed(2).toString() + "%" ;
                    } else {
                     
                        replacemet[k] =    value[k].toFixed(2) ;
                      
                    }
                
                   
                }
                
               
               
               arrJson.push(JSON.stringify(replacemet)) ;
              
               
            });

           console.log(arrJson);
Wara Haii
  • 202
  • 2
  • 5