我有一个对象,其中一对的值中有一个逗号。我想删除对象中所有此类值的逗号并返回修改后的对象。对象如下——
var obj = [
{
id: 1,
Product1: "Table",
Phone1: "9878987",
Price:"21,000"},
{
id: 2,
Product1: "Chair",
Phone1: "9092345",
Price:"23,000"},
{
id: 3,
Product1: "Cupboard",
Phone1: "9092345",
Price:"90,000"}
];
alert(JSON.stringify(obj));
我想删除价格值中的逗号(例如- 23,000 ==> 23000)。如何才能做到这一点?
您可以使用Array.prototype.map() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map创建一个新数组并String.prototype.replace() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace从中删除逗号Price
财产
Code:
const obj = [{id: 1,Product1: "Table",Phone1: "9878987",Price:"21,000"},{id: 2,Product1: "Chair",Phone1: "9092345",Price:"23,000"},{id: 3,Product1: "Cupboard",Phone1: "9092345",Price:"90,000"}]
const result = obj.map(({ Price, ...rest }) => ({
...rest,
Price: Price.replace(/,/, '')
}))
console.log(result)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)