1

I am trying to build a new array from json objects, taken from a googlespreadsheet api.

What I get looks like the following:

valueRanges:
    0:
        values:
            0:
              0: Tuesday May 12
            1: 
              0: ''
              1: ''
              2: ''
              3: 'Person 1' 
              4: 'Person 2'
            2:
              0: '00:00'
              1: 'till'
              2: '01:00'
              3: 'name 1'
              4: 'name 2'
            3:
              0: '01:00'
              1: 'till'
              2: '02:00'
              3: 'another name 1'
              4: 'another name 2'
            4:
              ...
    1:
        values:
            0:
              .....

What I would like the new array to look like:

[{
    date: 'Tuesday May 12, 
    header: true,
    subitems: [{
        0: {
           time: '',
           person1: 'Person 1',
           person2: 'Person 2'
        }
        1: {
           time: '00:00 till 01:00',
           person1: 'Name 1',
           person2: 'Name 2'
        }
        2: {
           time: '01:00 till 02:00',
           person1: 'Another Name 1',
           person2: 'Another Name 2'
        }
    }]
}]

I know how to get the time from 3 columns to 1 string, so that is not the issue.

The code I am currently using to create the array is the following:

fetch(url)
    .then((response) => response.json())
    .then((response) => {
        const data = [];
        for (let i = 0; i < response.valueRanges.length; i++) {
            const data = [{
                'date': response.valueRanges[i].values[0][0],
                'header':true,
            }];
            for (let j = 0; j < response.valueRanges[i].values.length; j++) {
                if (j == 0) {
                    continue;
                }
                const time = [response.valueRanges[i].values[j][0],response.valueRanges[i].values[j][1],response.valueRanges[i].values[j][2]];
                const array2 = [
                    {
                        'subitems': {
                            'time': time.join(' '),
                            'person1': response.valueRanges[i].values[j][3],
                            'person2':response.valueRanges[i].values[j][4]
                        }
                    }
                ]
            }
            data.push(array2);
        }

But I am not getting the correct array.

Edited

This is what I get:

[ { date: 'Tuesday May 12', header: true } ]
[ { subitems: { time: '', person1: 'Persoon 1', person2: 'Persoon 2' } } ]
[ { subitems: 
     { time: '00:00 tot 01:00',
       person1: 'Name 1',
       person2: 'Name 2' } } ]
[ { subitems: 
     { time: '01:00 tot 02:00',
       person1: 'Another name 1',
       person2: 'Another name 2' } } ]

What do I miss?

Extra question

And what if I want to change the new array to the following instead:

[
    {
        date: 'Tuesday May 12, 
        header: true
    }
    {
        time: '',
        person1: 'Person 1',
        person2: 'Person 2'
    }
    {
        time: '00:00 till 01:00',
        person1: 'Name 1',
        person2: 'Name 2'
    }
    {
        time: '01:00 till 02:00',
        person1: 'Another Name 1',
        person2: 'Another Name 2'
    }
]

So basically removing the subitems and just repeat the whole block depending on the dates.

2
  • You say what you are expecting, but please give an example of what you are actually getting. Commented May 6, 2020 at 18:23
  • @Ashley, I added an example of what I am currently getting. Commented May 6, 2020 at 18:28

2 Answers 2

1

So two points:

  1. You have declared two data arrays. I'm not sure what the first one is for, so I removed it.
  2. Some items are arrays that should be plain objects or vice-versa, so I changed that.
    fetch(url)
        .then((response) => response.json())
        .then((response) => {
            for (let i = 0; i < response.valueRanges.length; i++) {
                const data = {
                    date: response.valueRanges[i].values[0][0],
                    header: true,
                    subitems: []
                };
                for (let j = 0; j < response.valueRanges[i].values.length; j++) {
                    if (j == 0) {
                        continue;
                    }
                    const time = [response.valueRanges[i].values[j][0],response.valueRanges[i].values[j][1],response.valueRanges[i].values[j][2]];
                    const subitem = {
                         time: time.join(' '),
                         person1: response.valueRanges[i].values[j][3],
                         person2: response.valueRanges[i].values[j][4]
                    };
                    data.subitems.push(subitem);
                }
            }

An object with an array of subitems is now stored in data.

Extra question:

    fetch(url)
        .then((response) => response.json())
        .then((response) => {
            for (let i = 0; i < response.valueRanges.length; i++) {
                const data = [];
                data.push({
                    date: response.valueRanges[i].values[0][0],
                    header: true
                });
                for (let j = 0; j < response.valueRanges[i].values.length; j++) {
                    if (j == 0) {
                        continue;
                    }
                    const time = [response.valueRanges[i].values[j][0],response.valueRanges[i].values[j][1],response.valueRanges[i].values[j][2]];
                    const subitem = {
                         time: time.join(' '),
                         person1: response.valueRanges[i].values[j][3],
                         person2: response.valueRanges[i].values[j][4]
                    };
                    data.push(subitem);
                }
            }
Sign up to request clarification or add additional context in comments.

4 Comments

the first data array was there to be used outside the for loop. But 2 of the same arrays would not make sense indeed.
I fixed it by adding an extra array above the first for loop and using a array.push(data) just before the closing tag of the loop.
I added an extra question, what if I want to remove the extra subitems as array and just continue them as "row" and continue that whole block depending on the amount of dates.
Very simple edits to do so, take a look at my answer - I added an updated version at the bottom.
0

You need a few changes in the code. Here is the updated code..

const outerdata = [];
const innerdata = [];
for (let i = 0; i < response.valueRanges.length; i++) {
    const data = [{
        'date': response.valueRanges[i].values[0][0],
        'header':true,
        'subitems': []
    }];
    for (let j = 0; j < response.valueRanges[i].values.length; j++) {
        if (j == 0) {
            continue;
        }
        const time = [response.valueRanges[i].values[j][0],response.valueRanges[i].values[j][1],response.valueRanges[i].values[j][2]];
        const subitems = {
            'time': time.join(' '),
            'person1': response.valueRanges[i].values[j][3],
            'person2':response.valueRanges[i].values[j][4]
        }
        innerdata.push(subitems);
    }
    data.subitems = innerdata
    outerdata.push(data);
}

A quick review of the changes I did:

  1. Declared 2 arrays to hold the main array and the inner subitems arrays respectively.

    const outerdata = [];
    const innerdata = [];
    
  2. subitems array should be formed inside the inner loop and main array inside the outer loop

  3. The subitems array formed in the inner loop should be appended to the element of the main array

    data.subitems = innerdata
    

I hope it helps. Happy coding!

2 Comments

Thanks Deepak, I tried it, but it only shows the date once (basically the first one in the data), but the date is also repeating with the whole block.
You're welcome! @udarts please close this question if you have got the answer

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.