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.

3
  • 5
    if ((k !== "sRatio") || (k !== "name")) will always be true. Just use if (k == "name") {} else if (k == "sRatio") {} else {} Commented Jul 25, 2018 at 9:36
  • || should be &&. See stackoverflow.com/questions/26337003/… Commented Jul 25, 2018 at 9:44
  • 1
    You use replacer parameter in wrong way. It must return value, otherwise a property is not included. Commented Jul 25, 2018 at 9:45

3 Answers 3

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;
  }
})
Sign up to request clarification or add additional context in comments.

Comments

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);
    }
  }
})

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.