2016-10-18 10 views
-5

私はペアを持つオブジェクトの中にコンマを持つ値を持っています。このコンマをオブジェクトのすべての値に対して削除し、変更されたオブジェクトを返す必要があります。オブジェクトは次のとおりです。 -オブジェクトのプロパティ値からカンマを取り除く

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)でカンマを削除します。これはどうすればできますか?

+1

あなたはこれまでに何を試しましたか? – Rajesh

答えて

0

それが動作します、これを試してみてください:

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"}  
     ]; 

for (var i in obj) { 
    var Price = obj[i].Price.replace(',',''); 
    obj[i].Price = Price; 
}   

    console.log(obj); 

ワーキングフィドル:https://jsfiddle.net/pn3u8pdh/

1

あなたはすべてのアイテムを反復処理し、item.Priceを変更するArray.prototype.forEach()を使用することができます。

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"}]; 
 

 
obj.forEach(function(item) { 
 
    item.Price = item.Price.replace(/,/, ''); 
 
});  
 

 
console.log(obj);

0

置き換えるためにRegExを使用できます。これは、文字列の任意の数のカンマに対して機能するはずです。

var obj = [{ 
 
    id: 1, 
 
    Product1: "Table", 
 
    Phone1: "9878987", 
 
    Price: "1,21,000" 
 
}, { 
 
    id: 2, 
 
    Product1: "Chair", 
 
    Phone1: "9092345", 
 
    Price: "23,000" 
 
}, { 
 
    id: 3, 
 
    Product1: "Cupboard", 
 
    Phone1: "9092345", 
 
    Price: "90,000" 
 
}]; 
 

 
var modifiedArray = obj.map(function(currentObj) { 
 
    var replaceRegex = new RegExp(",", "g"); 
 
    currentObj.Price = currentObj.Price.replace(replaceRegex, ""); 
 
    return currentObj; 
 
}); 
 
document.querySelector("#result").innerHTML = 
 
    JSON.stringify(modifiedArray);
<div id="result"> 
 
</div>

0

あなたはループを使用せずに、この使用して正規表現を行うことができます。

var obj= ... //your array 
obj= JSON.stringify(obj); 
obj= obj.replace(/(?=,(?!"))(,(?!{))/g,""); 
obj= JSON.parse(obj) //you get you object without , in between your values 
関連する問題