在第一个元素上添加双引号

时间:2019-01-17 11:17:28

标签: javascript

我的时间戳没有双引号,我需要在第一个元素(所有时间戳)上加上双引号

我尝试使用replace,但错误提示:chaine.replace不是函数

    var globalTab = [];
    globalTab.push(TabJSON)
    console.log(globalTab)
    // OUTPUT 
    [[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
        {Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
        {Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
        {Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
        {Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}],
    [{Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
        {Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
        {Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
        {Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
        {Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}]]

chaine = globalTab
chaine=chaine.replace(/(\d{4}-\d{2}[^,]+,)/g,'"$1"');
console.log(chaine)
// Error

预期结果:

  [[{Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
        {Timestamp: "2019-01-15T08:32:00.000Z", Average: 200},
        {Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
        {Timestamp: "2019-01-15T08:29:00.000Z", Average: 200},
        {Timestamp: "2019-01-15T08:35:00.000Z", Average: 200}],
    [{Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
        {Timestamp: "2019-01-15T08:32:00.000Z", Average: 200},
        {Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
        {Timestamp: "2019-01-15T08:29:00.000Z", Average: 200},
        {Timestamp: "2019-01-15T08:35:00.000Z", Average: 200}]]

2 个答案:

答案 0 :(得分:0)

我不知道这是否是最有效的方法,但是我以这种方式尝试,因为我也不知道它是一个字符串还是数组数组。您正在使用replace,就像它是一个字符串一样,但是它看起来像一个数组,即使您没有看到引号,如果它是一个对象,它们也会隐式存在。无论如何。有了这个,我认为它将为您工作。

    var newGlobalTab=[];
    for (var i=0;i<globalTab.length;i++){
        var elem=[]
        for (var j in globalTab[i]){
            elem.push({Timestamp:'"'+globalTab[i][j]["Timestamp"]+'"', Average:globalTab[i][j]["Average"]});
        }
        newGlobalTab.push(elem);
    }
    console.log(newGlobalTab);

我希望它对您有帮助,对不起我无法采用最有效的方式。也为我的英语感到抱歉,我以前会说西班牙语。

答案 1 :(得分:0)

首先,您需要将其转换为字符串...因此,在我的控制台中,我对此进行了测试:

var globalTab = `[[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
    {Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
    {Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
    {Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
    {Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}],
[{Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
    {Timestamp: 2019-01-15T08:32:00.000Z, Average: 200},
    {Timestamp: 2019-01-15T08:26:00.000Z, Average: 200},
    {Timestamp: 2019-01-15T08:29:00.000Z, Average: 200},
    {Timestamp: 2019-01-15T08:35:00.000Z, Average: 200}]]`

之后,将您的替换项修改为此:

globalTab = globalTab.replace(/(\d{4}-\d{2}[^,]+Z)/g,'"$1"');

以及您期望的结果(可以在浏览器控制台中进行测试):

"[[{Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
    {Timestamp: "2019-01-15T08:32:00.000Z", Average: 200},
    {Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
    {Timestamp: "2019-01-15T08:29:00.000Z", Average: 200},
    {Timestamp: "2019-01-15T08:35:00.000Z", Average: 200}],
[{Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
    {Timestamp: "2019-01-15T08:32:00.000Z", Average: 200},
    {Timestamp: "2019-01-15T08:26:00.000Z", Average: 200},
    {Timestamp: "2019-01-15T08:29:00.000Z", Average: 200},
    {Timestamp: "2019-01-15T08:35:00.000Z", Average: 200}]]"

希望对您有帮助。