Change the json value

Asked

Viewed 257 times

1

I have the following JSON:

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

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

1 answer

1


You can use the .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 that string, and Number to convert to number. If you want you can still use string, ignoring the Number, depends on how you use these values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.