0

I'm new to javascript and need to iterate through a JSON list of dictionaries to create an list for each key.

Here's my JSON:

[
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Color Correction",
        "Status": "In Progress",
        "Completion": "60"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Conform",
        "Status": "In Progress",
        "Completion": "70"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 2",
        "Job": "Scanning",
        "Status": "Complete",
        "Completion": "100"
    },
    {
        "Date": "Aug. 12, 2015",
        "Reel": "Reel 1",
        "Job": "QC only",
        "Status": "In Progress",
        "Completion": "50"
    }
]

I'm using this function to read in JSON:

$.getJSON('cgi-bin/fakedata.txt', function(main)

I want to be able to access main['Date'] = ['August 12, 2015','August 14, 2015']

etc...

0

3 Answers 3

1

Below is a runnable node program. It looked like to me you were trying to get the dates into a separate array. So you could access them like dates[0].

#!/usr/bin/node

json = [
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Color Correction",
        "Status": "In Progress",
        "Completion": "60"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Conform",
        "Status": "In Progress",
        "Completion": "70"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 2",
        "Job": "Scanning",
        "Status": "Complete",
        "Completion": "100"
    },
    {
        "Date": "Aug. 12, 2015",
        "Reel": "Reel 1",
        "Job": "QC only",
        "Status": "In Progress",
        "Completion": "50"
    }
];


function getDates(main) {
    dates = [];
    for (var i = 0; i < main.length; i++ ) {
        dates.push(main[i].Date);
    }
    return dates;
};

dates = getDates(json);
console.log(dates);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is just what I needed to get started.
0
function fieldValues(main, field) {
  var res = {};
  for(item of main) {
    res[item[field]] = 0;
  }
  return Object.keys(res);
}

var main = [
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Color Correction",
        "Status": "In Progress",
        "Completion": "60"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 1",
        "Job": "Conform",
        "Status": "In Progress",
        "Completion": "70"
    },
    {
        "Date": "Aug. 14, 2015",
        "Reel": "Reel 2",
        "Job": "Scanning",
        "Status": "Complete",
        "Completion": "100"
    },
    {
        "Date": "Aug. 12, 2015",
        "Reel": "Reel 1",
        "Job": "QC only",
        "Status": "In Progress",
        "Completion": "50"
    }
];

console.log(fieldValues(main, "Date"));

Comments

0

This should work

 var x = JSON.stringify(json,["date"]);

You can then make it a JSON.

2 Comments

your array would be emtpy.
I'm assuming that json binds to his json data.

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.