简体   繁体   English

Javascript使数据结构具有动态变量

[英]Javascript make data structure with dynamic variables

[
    {
        "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": []
    }
]

How to make such data structure with dynamic variables in Javascript? 如何使用Javascript中的动态变量制作此类数据结构? Lets say: I need to change periods[start] and periods[end] times in day:6 or day:2 I want to make new data2 structure like data,but with my variables of time. 可以说:我需要在day:6或day:2中更改period [start]和period [end]时间,我想创建像data一样的新data2结构,但要使用时间变量。 something like this : 像这样的东西:

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

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

}

It's a bit unclear what you're trying to achieve. 尚不清楚您要实现的目标。 For example, the value of the property "periods" is an array. 例如,属性“ periods”的值是一个数组。 So, periods[start] doesn't make a lot of sense, because it's not quite clear are you trying to update all periods with a new value for start property or just a particular period or what? 因此, periods[start]并没有多大意义,因为还不清楚您是要使用start属性的新值还是仅针对特定期间或什么来更新所有期间?

It might be a good idea to convert your JSON into a dictionary , so you can do something like: 将JSON转换为字典可能是一个好主意,因此您可以执行以下操作:

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

I've left some question marks intentionally, for you to see that periods is an array, and you have to deal with it in that fashion. 我故意留下了一些问号,以便您看到句点是一个数组,您必须以这种方式处理它。

Please have a look at the below test code. 请看下面的测试代码。

You can easily understand it. 您可以轻松理解它。 Finally, I have used your provided code after this. 最后,在此之后,我使用了您提供的代码。 The code focuses on concatenation of variables and strings. 该代码着重于变量和字符串的串联。

There are other methods too. 还有其他方法。 You can comment if you need more help. 如果您需要更多帮助,可以发表评论。

> 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
>

Note: Make sure, you have properly used quotes ' and " . 注意:确保已正确使用引号'"

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

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

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