Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  73] [ 7]  / answers: 1 / hits: 51275  / 6 Years ago, thu, august 2, 2018, 12:00:00

How can I append a JSON like structure while iterating through a for loop?



For Example (Pseudo Code):



var i;
for (i = 0; i < clients.length; i++) {
date = clients.date;
contact = clients.contact;
}


My main goal is to append as many groups of: dates and contacts as the clients.length data holds.



I need each loop iteration to create something like below of multiple indexes of date and contact groups. My overall goal is to have a data structure like below created through my for loop.



Assume im just using strings for: Date & Contact



 var data = [
{
Date: 2015-02-03,
Contact: 1
},
{
Date: 2017-01-22,
Contact: 2

}
];

More From » arrays

 Answers
61
var data = []

function Client(date, contact) {
this.date = date
this.contact = contact
}

clients = new Array();

for (i = 0; i < 4; i++) {
clients.push(new Client(2018-08-0 + i, i))
}

for (i = 0; i < clients.length; i++) {
var dict = {}
dict['Date'] = clients[i].date
dict['Contact'] = clients[i].contact
data[i] = dict
}

console.log(data)

[#53826] Monday, July 30, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kieraelsies

Total Points: 718
Total Questions: 103
Total Answers: 104

Location: England
Member since Sun, May 21, 2023
1 Year ago
kieraelsies questions
Tue, Aug 3, 21, 00:00, 3 Years ago
Tue, Feb 23, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Mon, Sep 16, 19, 00:00, 5 Years ago
;