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

210
Vistas
convert value of json object from string in array to number using floatparse

I have a array with json objects. some objects properties values I need to convert to number from string. I need to push converted number value json to new list. I tried using floatparse but didnt succeed. code

var json = [{
    "id" : "0", 
    "msg"   : "hi",
    "cost" : "250.24",
    "discount": "10"
},
{
    "id" : "1", 
    "msg"   : "there",
    "cost" : "45.00",
    "discount": "0"
}];

var  json1 = [];

for (var key in json) {
       if (json.hasOwnProperty(key)) {
    
          for (const prop in json[key]) {        
          const parsed = parseFloat(json[key][prop]);              
          res[key][prop] = isNaN(parsed) ? json[key][prop] : parsed;                                        
           //  need to  push  array of modified json object value converted to number from string into json1  
          }
         
       }
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

As objects

I prefer a map solution:

var json = [{
    "id": "0",
    "msg": "hi",
    "cost": "250.24",
    "discount": "10"
  },
  {
    "id": "1",
    "msg": "there",
    "cost": "45.00",
    "discount": "0"
  }
];

const json1 = json.map(obj =>
  Object.fromEntries(Object.entries(obj).map(([key, value]) => {
    const parsedValue = parseFloat(value);
    return [
      key,
      isNaN(parsedValue) ? value : parsedValue
    ];
  })));

console.log(json1);

To take it back to loops however:

var json = [{
    "id": "0",
    "msg": "hi",
    "cost": "250.24",
    "discount": "10"
  },
  {
    "id": "1",
    "msg": "there",
    "cost": "45.00",
    "discount": "0"
  }
];

const json1 = [];

for (const obj of json) {
  const mappedObj = {};

  for (const [key, value] of Object.entries(obj)) {
    const parsedValue = parseFloat(value);
    mappedObj[key] = isNaN(parsedValue) ? value : parsedValue;
  }

  json1.push(mappedObj);
}

console.log(json1);

As arrays

var json = [[
    {key:"id", value:"0"},
    {key:"msg", value:"hi"},
    {key:"cost", value:"250.24"},
    {key:"discount", value:"10"}
  ],
  [
    {key:"id", value:"1"},
    {key:"msg", value:"there"},
    {key:"cost", value:"45.00"},
    {key:"discount", value:"0"}
  ]
];

const json1 = json.map(obj =>
  obj.map(({key, value}) => {
    const parsedValue = parseFloat(value);
    return {
      key,
      value: isNaN(parsedValue) ? value : parsedValue
    };
  }));

console.log(json1);

about 4 years ago · Juan Pablo Isaza Denunciar

0

your loop are wrong.

for (var key in json) => when you use loop over an array it's return item index not key

json.hasOwnProperty(key) => your json is an array you don't need to use hasOwnProperty it's for object

I wrote 2 way for you:

*: you can use + for convert string to number.

*: if you want loop over item keys you can use way1, check Object.entries, Object.keys, Object.values methods.

const json = [{
    "id" : "0", 
    "msg"   : "hi",
    "cost" : "250.24",
    "discount": "10"
},
{
    "id" : "1", 
    "msg"   : "there",
    "cost" : "45.00",
    "discount": "0"
}];

//way1
const json1 = json.map((item)=>{
    return Object.entries(item).reduce((pre,[key,value])=>{
        const parsed = +value
        pre[key] = isNaN(parsed) ? value : parsed
        return pre
    },{})
})
console.log('json1',json1)

//way2
const json2 = json.map((item)=>{
    return {
        "id": +item.id,
        "msg": item.msg,
        "cost": +item.cost,
        "discount": +item.discount,
    }
})
console.log('json2',json2)

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