Change json value

1

I have the following JSON:

[{"id": 1,"preco": "R$50"}, {"id": 2,"preco": "R$70"}]

I would like to change the value of 'price' by removing R$ leaving only numbers using javascript or jQuery

    
asked by anonymous 16.11.2017 / 17:05

1 answer

1

You can use .map() to do this, an example would be:

const arr = [{"id": 1, "preco": "R$50"}, {"id": 2, "preco": "R$70"}];
const formatado = arr.map(obj => {
  return {
    id: obj.id,
    preco: Number(obj.preco.slice(2))
  };
});

console.log(formatado);

The idea is to use .slice(2) to remove the first two characters of this string, and Number to convert to number. If you want you can use string in it, ignoring Number , depends on the use you will give those values.

    
16.11.2017 / 17:09