Javascript使用动态变量创建数据结构

时间:2018-05-16 12:19:55

标签: javascript

[
    {
        "day": 0,
        "periods": [
            {
                "start": "01:00",
                "end": "02:00",
                "title": "",
                "backgroundColor": "rgba(254, 0, 0, 0.7)",
                "borderColor": "rgb(42, 60, 255)",
                "textColor": "rgb(0, 0, 0)"
            },
            {
                "start": "02:30",
                "end": "03:00",
                "title": "",
                "backgroundColor": "rgba(254, 0, 0, 0.7)",
                "borderColor": "rgb(42, 60, 255)",
                "textColor": "rgb(0, 0, 0)"
            }
        ]
    },
    {
        "day": 1,
        "periods": [
            {
                "start": "01:00",
                "end": "02:00",
                "title": "",
                "backgroundColor": "rgba(254, 0, 0, 0.7)",
                "borderColor": "rgb(42, 60, 255)",
                "textColor": "rgb(0, 0, 0)"
            }
        ]
    },
    {
        "day": 2,
        "periods": [
            {
                "start": "01:00",
                "end": "01:30",
                "title": "",
                "backgroundColor": "rgba(254, 0, 0, 0.7)",
                "borderColor": "rgb(42, 60, 255)",
                "textColor": "rgb(0, 0, 0)"
            }
        ]
    },
    {
        "day": 3,
        "periods": [
            {
                "start": "02:00",
                "end": "02:30",
                "title": "",
                "backgroundColor": "rgba(254, 0, 0, 0.7)",
                "borderColor": "rgb(42, 60, 255)",
                "textColor": "rgb(0, 0, 0)"
            }
        ]
    },
    {
        "day": 4,
        "periods": [
            {
                "start": "01:00",
                "end": "01:30",
                "title": "",
                "backgroundColor": "rgba(254, 0, 0, 0.7)",
                "borderColor": "rgb(42, 60, 255)",
                "textColor": "rgb(0, 0, 0)"
            }
        ]
    },
    {
        "day": 5,
        "periods": [
            {
                "start": "01:30",
                "end": "02:00",
                "title": "",
                "backgroundColor": "rgba(254, 0, 0, 0.7)",
                "borderColor": "rgb(42, 60, 255)",
                "textColor": "rgb(0, 0, 0)"
            },
            {
                "start": "03:00",
                "end": "03:30",
                "title": "",
                "backgroundColor": "rgba(254, 0, 0, 0.7)",
                "borderColor": "rgb(42, 60, 255)",
                "textColor": "rgb(0, 0, 0)"
            }
        ]
    },
    {
        "day": 6,
        "periods": []
    }
]

如何在Javascript中使用动态变量制作这样的数据结构? 让我们说: 我需要在白天更改句点[开始]和句点[结束]时间:6或日:2 我想创建像数据一样的新数据2结构,但是我的时间变量。 像这样的东西:

for (var i=0; i <arr.legth; ++i){

    data2="'[{"day":MYVARIABLE1,"periods":[{"start":"MYVARIABLE2","end":"MYVARIABLE3",  ...}]';

}

2 个答案:

答案 0 :(得分:0)

有点不清楚你想要实现的目标。例如,属性&#34;期间&#34;是一个数组。因此,periods[start]并没有多大意义,因为您不太清楚您是否尝试使用start属性的新值或仅在特定时段更新所有期间或者什么?

convert your JSON into a dictionary可能是个好主意,所以你可以这样做:

var day = 2;
data[day].periods[???].start = "01:00";

我故意留下了一些问号,因为你看到那些句号是一个数组,你必须以这种方式处理它。

答案 1 :(得分:0)

请查看以下测试代码。

您可以轻松理解它。最后,我在此之后使用了您提供的代码。该代码侧重于变量和字符串的连接。

  

还有其他方法。如果您需要更多帮助,可以发表评论。

> var a = 67
undefined
> var name = 'JavaScript'
undefined
>
> a + ""
'67'
> a + name
'67JavaScript'
> "'" + name + "'"
'\'JavaScript\''
>
> '"' + name + '"'
'"JavaScript"'
>
> var obj = '{ "name": "Rishikesh", "age": 26}'
undefined
> obj
'{ "name": "Rishikesh", "age": 26}'
>
> typeof obj
'string'
>
> // Coversion in real JS object
undefined
> realObj = JSON.parse(obj);
{ name: 'Rishikesh', age: 26 }
>
> realObj.name
'Rishikesh'
>
> realObj.age
26
>
  

注意:请确保您使用了引号'"

// A sample test code 
var arr = [67]; // Array of 1 item

var MYVARIABLE1 = 'Wednesday'
var MYVARIABLE2 = '01:05'
var MYVARIABLE3 = '02:06'
var data2 = null;

for (var i=0; i < arr.length; ++i){
    data2 = '[{' +
            '"day": "' + MYVARIABLE1 + '",' + 
            '"periods":[{' +
                    '"start":"' + MYVARIABLE2 + '",' +
                    '"end":"' + MYVARIABLE3 + '"' +
                    '}]' + 
          '}]';
}

console.log(data2)
// [{"day": "Wednesday","periods":[{"start":"01:05","end":"02:06"}]}]