简体   繁体   中英

How do I remove $ sign from string using replace?

I cannot remove the dollar sign from the balance properties, I am not sure what I am missing? I thought it was string.replace(searchvalue, newvalue)??

 const data = [{ index: "14", name: "Bob", balance: "$100", }, { index: "23", name: "John", balance: "$120", }, { index: "17", name: "Steve", balance: "$80", }, ]; const balances = data.map((amount) => { var newAmount = amount.replace("$", ""); console.log(newAmount); });

What I want:

100
120
80

You just forgot to get the balance property of each item.

 const data = [{ index: "14", name: "Bob", balance: "$100", }, { index: "23", name: "John", balance: "$120", }, { index: "17", name: "Steve", balance: "$80", }, ]; data.forEach((item) => { var newAmount = item.balance.replace("$", ""); console.log(newAmount); });

I just forgot the.balance thanks yall

const data = [{
    index: "14",
    name: "Bob",
    balance: "$100",
  },
  {
    index: "23",
    name: "John",
    balance: "$120",
  },
  {
    index: "17",
    name: "Steve",
    balance: "$80",
  },
];

const balances = data.map((amount) => {
  var newAmount = amount.balance.replace("$", "");
  console.log(newAmount);
});

check the following code

 const data = [{ index: "14", name: "Bob", balance: "$100", }, { index: "23", name: "John", balance: "$120", }, { index: "17", name: "Steve", balance: "$80", }, ]; const modifiedData = data.map((amount) => { const oldBalance=amount.balance;// here we cache old value amount.balance=oldBalance.replace("$", "");// here we assign the modified value console.log(amount.balance); return amount; }); console.log(modifiedData);

Please work with this code snippet. I hope this code works well with you.

const balances = data.map((amount) => {
    const newAmount = amount[balance].replace("$", "");
    console.log(newAmount)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM