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.
-
You say what you are expecting, but please give an example of what you are actually getting.Ashley– Ashley2020年05月06日 18:23:50 +00:00Commented May 6, 2020 at 18:23
-
@Ashley, I added an example of what I am currently getting.udarts– udarts2020年05月06日 18:28:50 +00:00Commented May 6, 2020 at 18:28
2 Answers 2
So two points:
- You have declared two data arrays. I'm not sure what the first one is for, so I removed it.
- 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);
}
}
4 Comments
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:
Declared 2 arrays to hold the main array and the inner subitems arrays respectively.
const outerdata = []; const innerdata = [];subitems array should be formed inside the inner loop and main array inside the outer loop
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!