admin管理员组

文章数量:1277396

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

    }
];

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

    }
];
Share Improve this question edited Aug 2, 2018 at 1:04 nil asked Aug 2, 2018 at 1:00 nilnil 571 gold badge1 silver badge7 bronze badges 6
  • developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Max Baldwin Commented Aug 2, 2018 at 1:02
  • why not just create an array of objects then use JSON.stringify - the problem with your pseudo code is that it doesn't explain a thing – Jaromanda X Commented Aug 2, 2018 at 1:04
  • thats a great idea but how can i append each stringify value in an array. That would give me the same structure as the data var? – nil Commented Aug 2, 2018 at 1:07
  • you don't ... you create an array of objects, then stringify it once you're done - clearly you have an existing object, something like: clients = [ { date:'2015-02-03', contact: 1 },{ date:'2017-01-22', contact: 2 } ]; so siimply result = JSON.stringify(clients.map(({date, contact}) => ({Date:date, Contact:contact}))) – Jaromanda X Commented Aug 2, 2018 at 1:08
  • @nil can you post data of clients array? – Vikasdeep Singh Commented Aug 2, 2018 at 1:08
 |  Show 1 more ment

3 Answers 3

Reset to default 2
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)

It's a simple push object to array operation. Please try below

var data=[];

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

(ES6) You can use map function to extract the data you wish, and then convert it to json.

let clients = [{date:"", contact:"", otherstuff:""}, {date:"", contact:"", otherstuff:""}]
let clientsMapped = clients.map(client => ({Date:client.date, Contact:client.contact}))
let yourJson = JSON.stringify(clientsMapped)

本文标签: arraysJavascript Append JSON ObjectsStack Overflow