简体   繁体   English

无法从值对中删除双引号

[英]Unable to remove the double quotes from the value pair

I am getting the date data and setting the date into the JSON object 我正在获取日期数据并将日期设置为JSON对象

let lastSeven = moment().subtract(7, 'd').toDate();
var dat =  moment(lastSeven).format('D');

dat = dat.replace(/^"(.*)"$/, '$1');

I have to get the data like this 我必须得到这样的数据

{
  'year': 2018,
  'month': 9,
  'day': 14
};

But after the replace also I am getting 但是更换后我也得到了

{
  'year': "2018",
  'month': "9",
  'day': "14"
};

How can I remove the double quotes from the values ? 如何从值中删除双引号?

You need convert string to number. 您需要将字符串转换为数字。

for(var key in data){
    data[key] = Number(data[key]);
}

well with out the double quotes they would be numbers, so convert all value to number. 如果没有双引号,它们将是数字,因此请将所有值转换为数字。

 let obj = { 'year': 2018, 'month': 9, 'day': 14 }; for (let property in obj) { if (obj.hasOwnProperty(property)) obj[property] = +obj[property]; } console.log(obj); 

You can try like this way using Array.prototype.map() 您可以使用Array.prototype.map()这样尝试

 var myObject = { 'year': "2018", 'month': "9", 'day': "14" }; var result = {}; Object.keys(myObject).map(function(key, index) { result[key] = +myObject[key]; }); console.log(result) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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