0
[
    {
        "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? 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. something like this :

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

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

}
7
  • What would you hope to achieve with that? Commented May 16, 2018 at 12:22
  • I think you are trying to create the objects and then you have to append them to the main array. But what are you trying to achieve exactly? Commented May 16, 2018 at 12:23
  • I want to change periods[start]="01:00"] where day=2 Commented May 16, 2018 at 12:24
  • Parse it, then loop through it and find the things you need to change? Or just parse it and set your variable? Unless you give us some context there are 1000 ways to do this. Commented May 16, 2018 at 12:26
  • @Ele: not if data2 is a string... Commented May 16, 2018 at 12:26

2 Answers 2

0

It's a bit unclear what you're trying to achieve. For example, the value of the property "periods" is an array. 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?

It might be a good idea to convert your JSON into a dictionary, so you can do something like:

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.