Get JSON array (API) where object is in object with Javascript - javascript

I am using the code below to build a table based on an API and am fine when there is in object in an array (e.g. lineStatuses[0].statusSeverityDescription), however when there is an object, in an object, in an array, it does not work and I get the result [object Object] returned.
Here is a sample of the JSON data from the URL (I am expecting Undefined to be returned for the first record):
[
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "bakerloo",
"name": "Bakerloo",
"modeName": "tube",
"disruptions": [],
"created": "2016-06-03T12:36:54.19Z",
"modified": "2016-06-03T12:36:54.19Z",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"statusSeverity": 10,
"statusSeverityDescription": "Good Service",
"created": "0001-01-01T00:00:00",
"validityPeriods": []
}
],
"routeSections": [],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Bakerloo&serviceTypes=Regular"
}
]
},
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "central",
"name": "Central",
"modeName": "tube",
"disruptions": [],
"created": "2016-06-03T12:36:54.037Z",
"modified": "2016-06-03T12:36:54.037Z",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"lineId": "central",
"statusSeverity": 5,
"statusSeverityDescription": "Part Closure",
"reason": "CENTRAL LINE: Saturday 11 and Sunday 12 June, no service between White City and Ealing Broadway / West Ruislip. This is to enable track replacement work at East Acton and Ruislip Gardens. Replacement buses operate.",
"created": "0001-01-01T00:00:00",
"validityPeriods": [
{
"$type": "Tfl.Api.Presentation.Entities.ValidityPeriod, Tfl.Api.Presentation.Entities",
"fromDate": "2016-06-11T03:30:00Z",
"toDate": "2016-06-13T01:29:00Z",
"isNow": false
}
],
"disruption": {
"$type": "Tfl.Api.Presentation.Entities.Disruption, Tfl.Api.Presentation.Entities",
"category": "PlannedWork",
"categoryDescription": "PlannedWork",
"description": "CENTRAL LINE: Saturday 11 and Sunday 12 June, no service between White City and Ealing Broadway / West Ruislip. This is to enable track replacement work at East Acton and Ruislip Gardens. Replacement buses operate.",
"additionalInfo": "Replacement buses operate as follows:Service A: White City - East Acton - North Acton - West Acton - Ealing Common (for District and Piccadilly Lines) - Ealing BroadwayService B: White City - North Acton - Northolt - South Ruislip - Ruislip Gardens - West RuislipService C: White City - North Acton - Park Royal (Piccadilly Line) - Hanger Lane - Perivale - Greenford - Northolt",
"created": "2016-05-12T11:04:00Z",
"affectedRoutes": [],
"affectedStops": [],
"isBlocking": true,
"closureText": "partClosure"
}
}
],
"routeSections": [],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Central&serviceTypes=Regular"
}
]
}
]
I am also trying to use setInterval to refresh the tube-disruption DIV with updated data from the API, which is not workng. Below is the code (with the URL returning some of the JSON data above). Any ideas what I am doing wrong?
var xmlhttp = new XMLHttpRequest();
var url = "https://api.tfl.gov.uk/line/mode/tube/status";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunctionDisruption(xmlhttp.responseText);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
setInterval(myFunctionDisruption, 600000);
function myFunctionDisruption(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>";
for(i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].lineStatuses[0].disruption.description + <!-- DOES NOT WORK -->
"</td></tr>";
}
out += "</table>";
document.getElementById("tube-disruption").innerHTML = out;
}

The below code will generate a table for you. The generic tableMaker function takes an array of an object or an array of multiple objects provided in the first argument. All objects should have same keys (properties) since these keys are used to create the table header (if the second argument is set to true) and the values are used to create each row. It will return an HTML table text. You can see the tableMaker function working with a smaller size data at here. You can also practice it with some sample and simple data you may produce.
In your case you have multiple nested objects those, I guess, need to be converted into separate tables within the corresponding cells of the main table. For that purpose i have another function tabelizer which handles this job recursively by utilizing the tableMaker function. The result of tabelizer is a complete HTML text of your master table.
Lets see
var tableMaker = (o,h) => {var keys = o.length && Object.keys(o[0]),
rowMaker = (a,t) => a.reduce((p,c,i,a) => p + (i === a.length-1 ? "<" + t + ">" + c + "</" + t + "></tr>"
: "<" + t + ">" + c + "</" + t + ">"),"<tr>"),
rows = o.reduce((r,c) => r + rowMaker(keys.reduce((v,k) => v.concat(c[k]),[]),"td"),h ? rowMaker(keys,"th") : []);
return rows.length ? "<table>" + rows + "</table>" : "";
},
data = [
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "bakerloo",
"name": "Bakerloo",
"modeName": "tube",
"disruptions": [],
"created": "2016-06-03T12:36:54.19Z",
"modified": "2016-06-03T12:36:54.19Z",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"statusSeverity": 10,
"statusSeverityDescription": "Good Service",
"created": "0001-01-01T00:00:00",
"validityPeriods": []
}
],
"routeSections": [],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Bakerloo&serviceTypes=Regular"
}
]
},
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "central",
"name": "Central",
"modeName": "tube",
"disruptions": [],
"created": "2016-06-03T12:36:54.037Z",
"modified": "2016-06-03T12:36:54.037Z",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"lineId": "central",
"statusSeverity": 5,
"statusSeverityDescription": "Part Closure",
"reason": "CENTRAL LINE: Saturday 11 and Sunday 12 June, no service between White City and Ealing Broadway / West Ruislip. This is to enable track replacement work at East Acton and Ruislip Gardens. Replacement buses operate.",
"created": "0001-01-01T00:00:00",
"validityPeriods": [
{
"$type": "Tfl.Api.Presentation.Entities.ValidityPeriod, Tfl.Api.Presentation.Entities",
"fromDate": "2016-06-11T03:30:00Z",
"toDate": "2016-06-13T01:29:00Z",
"isNow": false
}
],
"disruption": {
"$type": "Tfl.Api.Presentation.Entities.Disruption, Tfl.Api.Presentation.Entities",
"category": "PlannedWork",
"categoryDescription": "PlannedWork",
"description": "CENTRAL LINE: Saturday 11 and Sunday 12 June, no service between White City and Ealing Broadway / West Ruislip. This is to enable track replacement work at East Acton and Ruislip Gardens. Replacement buses operate.",
"additionalInfo": "Replacement buses operate as follows:Service A: White City - East Acton - North Acton - West Acton - Ealing Common (for District and Piccadilly Lines) - Ealing BroadwayService B: White City - North Acton - Northolt - South Ruislip - Ruislip Gardens - West RuislipService C: White City - North Acton - Park Royal (Piccadilly Line) - Hanger Lane - Perivale - Greenford - Northolt",
"created": "2016-05-12T11:04:00Z",
"affectedRoutes": [],
"affectedStops": [],
"isBlocking": true,
"closureText": "partClosure"
}
}
],
"routeSections": [],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Central&serviceTypes=Regular"
}
]
}
],
tabelizer = (a) => a.length ? tableMaker(a.map(e => Object.keys(e).reduce((p,k) => (p[k] = Array.isArray(e[k]) ? tabelizer(e[k]) : e[k],p),{})),true)
: "",
tableHTML = tabelizer(data);
document.write(tableHTML);
I used arrow functions but they might not work at Safari or IE. You might need to convert them to the conventional function notation.
You can also try the code out at repl.it where you can see the HTML text displayed through console.log.

Related

Javascript: Parse array of json objects within a string

I have an array of json object within a string:
[
{
"title": "Ham on Rye",
"alternativeTitles": [],
"secondaryYearSourceId": 0,
"sortTitle": "ham on rye",
"sizeOnDisk": 0,
"status": "released",
"overview": "A bizarre rite of passage at the local deli determines the fate of a generation of teenagers, leading some to escape their suburban town and dooming others to remain…",
"inCinemas": "2019-08-10T00:00:00Z",
"images": [
{
"coverType": "poster",
"url": "http://image.tmdb.org/t/p/original/px7iCT1SgsOOSAXaqHwP50o0jAI.jpg"
}
],
"downloaded": false,
"remotePoster": "http://image.tmdb.org/t/p/original/px7iCT1SgsOOSAXaqHwP50o0jAI.jpg",
"year": 2019,
"hasFile": false,
"profileId": 0,
"pathState": "dynamic",
"monitored": false,
"minimumAvailability": "tba",
"isAvailable": true,
"folderName": "",
"runtime": 0,
"tmdbId": 527176,
"titleSlug": "ham-on-rye-527176",
"genres": [],
"tags": [],
"added": "0001-01-01T00:00:00Z",
"ratings": {
"votes": 5,
"value": 6.9
},
"qualityProfileId": 0
}
]
When I try to parse my array of json objects, it always returns a string, so when I try to access it by:
let data = JSON.parse(JSON.stringify(jsonData));
console.log(data[0].title)
it returns a specific character like "[", as if it was a string.
Te problem is that you are stringifying something that is already a JSON string.
Change this:
let data = JSON.parse(JSON.stringify(jsonData));
to this:
let data = JSON.parse(jsonData);

Fuse JS search results scoring doesn't make sense

I'm trying to use Fuse JS to do a fuzzy search on a list of strings but the results it's giving back don't make sense.
I've set up a playcode here as an example.
Here my list is
[
"Yoga with Adriene - Revolution",
"Brewdog beers",
"NBA teams seen live",
"Yoga with Adriene - DEDICATE - 30 days",
"Yoga with Adriene - home",
]
My code config is
const options = {
isCaseSensitive: false,
includeScore: true,
shouldSort: true,
};
When I search with search term 'yoga home'
The closest match returned is 'Brewdog beers' which is clearly the worst search result, I'm expecting it to return the 'Yoga with Adriene - home' result first with 2 words matching.
The results are:
[
{
"item": "Brewdog beers",
"refIndex": 1,
"score": 0.6932183529538054
},
{
"item": "Yoga with Adriene - Revolution",
"refIndex": 0,
"score": 0.6959441913889373
},
{
"item": "Yoga with Adriene - home",
"refIndex": 4,
"score": 0.6959441913889373
},
{
"item": "Yoga with Adriene - DEDICATE - 30 days",
"refIndex": 3,
"score": 0.7504597227725356
}
]
Where a score of 0 is an exact match.

Is there a way to de-dupe a javascript array and combine values of the data?

I'm using an ajax request to grab some XML data which I then need to push into a chart in fusioncharts.
The XML data is formatted as [time taken], [work done], [which team done for], [who did it] (see below).
I'm iterating over the XML and then building the array using the code below:
//Time Recorded
if (columnidchecker == 7781) {
timearray.push($j(this).find('displayData').text());
temp1 = $j(this).find('displayData').text();
}
//Type of Activity
if (columnidchecker == 7782) {
activityarray.push($j(this).find('displayData').text());
temp2 = $j(this).find('displayData').text();
}
//Team Done For
if (columnidchecker == 7783) {
subjectarray.push($j(this).find('displayData').text());
temp3 = $j(this).find('displayData').text();
}
//Name
if (columnidchecker == 7777) {
internalclientarray.push($j(this).find('displayData').text());
temp4 = $j(this).find('userDisplayName').text();
}
});
//PUSH INTO A NEW ARRAY WHICH CAN THEN BE SORTED AND DE-DUPED WITH TIME COMBINED AGAINST ACTIVITY / TEAM.
objectarray.push([temp1, temp2, temp3, temp4]);
This builds an array of entries from the XML which basically outputs to something which looks like this:
0: (4) ["1.50", "Ad-hoc queries or calls", "Team 1", "James"]
1: (4) ["2.50", "Ad-hoc queries or calls", "Team 1", "James"]
2: (4) ["1.00", "Advice", "Team 2", "James"]
3: (4) ["3.50", "Meeting (External 3rd Party)", "Team 1", "James"]
4: (4) ["1.20", "Administration", Team 2", "James"]
5: (4) ["5.50", "Advice", "Team 1", "John"]
I'm trying to build a chart in fusioncharts which needs the format as shown below (ignore foot stuffs - it's taken straight from the fusioncharts help pages!).
{
"chart": {
"theme": "fusion",
"caption": "Revenue split by product category",
"subCaption": "For current year",
"xAxisname": "Quarter",
"yAxisName": "Revenues (In USD)",
"showSum": "1",
"numberPrefix": "$"
},
"categories": [
{
"category": [
{
"label": "Q1"
},
{
"label": "Q2"
},
{
"label": "Q3"
},
{
"label": "Q4"
}
]
}
],
"dataset": [
{
"seriesname": "Food Products",
"data": [
{
"value": "11000"
},
{
"value": "15000"
},
{
"value": "13500"
},
{
"value": "15000"
}
]
},
{
"seriesname": "Non-Food Products",
"data": [
{
"value": "11400"
},
{
"value": "14800"
},
{
"value": "8300"
},
{
"value": "11800"
}
]
}
]
}
The problem i'm having is that I cannot work out how to take the array of data with times, activity, team, name and push them into categories.
I think the first step is to create a new array of names which can be pushed into the "Category" data field in fusioncharts.
I then need a way in which to take the times being recorded against each activity and for each team and make sure it's assigned to the right person within the stacked bar chart and combine the amount of time spent. (i.e. "James" spent a total of 4 hours doing "Ad Hoc Queries and Calls" for Team 1 but this is split across two time entries so I need a way in which to combine them into one.)
Any help on this would be massively appreciated.
I can de-dupe the names to create a new array by using the following code:
namesarray.push(temp4);
uniq = [...new Set(namesarray)];
but after that it starts getting pretty complicated.
Maybe this can help you along the way. It's probably not exactly in the form you want it, but it demonstrates how you could break the problem down into smaller parts.
Pseudo-code:
get the unique names.
get the unique "task" names (for lack of a
better word)
for each unique person name:
3.1. get the data rows for that person
3.2 for each of all unique tasks names:
find the person data rows matching the task name
sum the duration of those data rows
const testData = [
[
"1.50",
"Ad-hoc queries or calls",
"Team 1",
"James"
],
[
"2.50",
"Ad-hoc queries or calls",
"Team 1",
"James"
],
[
"1.00",
"Advice",
"Team 2",
"James"
],
[
"3.50",
"Meeting (External 3rd Party)",
"Team 1",
"James"
],
[
"1.20",
"Administration",
"Team 2",
"James"
],
[
"5.50",
"Advice",
"Team 1",
"John"
]
];
const columnIndexByName = {
TASK_DURATION: 0,
TASK_NAME: 1,
FOR_WHICH_TEAM: 2,
PERSON_DOING_TASK: 3
};
const sum = (acc, next) => acc + next;
const uniqueNames = [...new Set(testData.map(row => row[columnIndexByName.PERSON_DOING_TASK])) ];
const uniqueTaskNames = [...new Set(testData.map(row => row[columnIndexByName.TASK_NAME])) ];
let result = {};
uniqueNames.forEach(personName => {
const personDataRows = testData.filter(row => row[columnIndexByName.PERSON_DOING_TASK] === personName);
let taskDurationsByTaskName = {};
uniqueTaskNames.forEach(taskName => {
const taskRows = personDataRows.filter(row => row[columnIndexByName.TASK_NAME] === taskName);
const taskDurations = taskRows.map(row => Number.parseFloat( row[columnIndexByName.TASK_DURATION] ));
const taskTotalDuration = taskDurations.reduce(sum, 0);
taskDurationsByTaskName[taskName] = taskTotalDuration;
})
result[personName] = taskDurationsByTaskName;
})
const renderData = data => document.querySelector("#output").innerHTML = JSON.stringify(data, null, 2);
renderData(result);
<pre id="output"></pre>

Function is still executing after if Statment

I am running an IF statement that checks if a JSON response is empty, to execute the next function, however, its executing both. Below is a sample of the JSON response that has 3 objects in the "count_business" an example of my function is below. I am running a .length < 1 but it seems to not check that.
JSON
{
"count_business": 3,
"business": [
{
"id": "Business.3b725ab1-1eeb-4f11-bb41-0a97cdd5475a",
"name": "Dogwood Valley Press LLC",
"industry": [
"Sporting Goods, Hobby, Musical Instrument, and Book Stores"
],
"found_at_address": {
"id": "Location.86fc8e6b-6dfb-446d-9488-aae59fea6eb2",
"location_type": "Address",
"street_line_1": "1604 Lincoln Ln",
"street_line_2": null,
"city": "Rolla",
"postal_code": "65401",
"zip4": "2614",
"state_code": "MO",
"country_code": "US",
"lat_long": {
"latitude": 37.956933,
"longitude": -91.743859,
"accuracy": "RoofTop"
},
"is_active": true,
"delivery_point": "SingleUnit",
"link_to_business_start_date": "2016-06-12",
"link_to_business_end_date": null
},
My if statement
if(res.data.count_business < 1){
fetchGooglePlace(data)
}else{
console.log(chalk.white('RUNNING WHITEPAGES'));
for(let i =0; i < res.data.business.length; i++ ){
console.log(res.data.business[i].name);
console.log(res.data.business[i].found_at_address.street_line_1);
/*Second loop to fetch phone numbers from array in whitePages JSON */
for (let x = 0; x < res.data.business[i].phones.length; x++) {
console.log(res.data.business[i].phones[x].phone_number);
console.log(' ');
}
}
}
})
After executing the app, it runs the whitePages API and also the fetchGooglePlace function.

getting information from the inner depths of a JSON object

I have a JSON object from the CrunchBase API giving me a bunch of info from a given company. Right now I am trying to go through the JSON object and create a list of their investors. The investors can fall into one of three categories, "company", "financial_org", or "person". All three types will be appended to the same list, finalInvestorList
The script runs without error, but only produces a list of investors from the first listed round. I have logged everything I think might help. the logs are in same-line comments.
Basically my problem is that it is only looping through one time, and therefor only adding the investors from the first round. Any help would be greatly appreciated. Let me know if you need more info!
var investorList = function(data, num) {
var fundingRounds = data["funding_rounds"];
var finalInvestorList = []
console.log(fundingRounds.length) // 3
for (i=0; i < fundingRounds.length; i++) {
var investments = data["funding_rounds"][i]["investments"];
console.log(data["funding_rounds"][1]["investments"]); //correctly logs the index 1 round for spling (2nd round)
var round = data["funding_rounds"][i];
console.log('round' + i); //only logs round0, never loops around for round1, round2
for (i=0; i < investments.length; i++) {
var angelObject = round["investments"][i]["person"];
if (angelObject != null) {
console.log("angel fired"); //fires for "Mitch Blumenfeld"
var angel = angelObject["first_name"] + " " + angelObject["last_name"];
finalInvestorList[i] = angel;
}
var financialOrgObject = round["investments"][i]["financial_org"];
if (financialOrgObject != null) {
console.log("financial_org fired"); //fires for "Bucknell Venture Plan Competition"
console.log(financialOrgObject['name']); //Bucknell VPC
var financialOrg = financialOrgObject["name"]
finalInvestorList[i] = financialOrg
}
var companyObject = round['investments'][i]["company"];
if (companyObject != null) {
console.log('company fired'); //i haven't bothered with this yet.. just logging it so ill know if its firing
}
}
}
console.log(finalInvestorList); //["Bucknell Venture Plan Competition", "Mitch Blumenfeld"]
}
The JSON object represented by the data is as follows
I have shortened it with just the bit needed. The object inside the JSON response data is represented by data["funding_rounds"]
this data was retrieved using the crunch API and can be found in full form at http://api.crunchbase.com/v/1/company/spling.js
"funding_rounds":
[{"round_code": "seed",
"source_url": "",
"source_description": "",
"raised_amount": 50000.0,
"raised_currency_code": "USD",
"funded_year": 2011,
"funded_month": 2,
"funded_day": 1,
"investments":
[{"company": null,
"financial_org":
{"name": "Bucknell Venture Plan Competition",
"permalink": "bucknell-venture-plan-competition",
"image": null},
"person": null},
{"company": null,
"financial_org": null,
"person":
{"first_name": "Mitch",
"last_name": "Blumenfeld",
"permalink": "mitch-blumenfeld",
"image": null}}]},
{"round_code": "seed",
"source_url": "http://techcrunch.com/2011/09/08/dreamit-ventures-launches-its-fall-2011-philadelphia-class/",
"source_description": "",
"raised_amount": 25000.0,
"raised_currency_code": "USD",
"funded_year": 2011,
"funded_month": 9,
"funded_day": 1,
"investments":
[{"company": null,
"financial_org":
{"name": "DreamIt Ventures",
"permalink": "dreamit-ventures",
"image":
{"available_sizes":
[[[150,
57],
"assets/images/resized/0002/7773/27773v5-max-150x150.jpg"],
[[250,
96],
"assets/images/resized/0002/7773/27773v5-max-250x250.jpg"],
[[251,
97],
"assets/images/resized/0002/7773/27773v5-max-450x450.jpg"]],
"attribution": null}},
"person": null}]},
{"round_code": "a",
"source_url": "http://techcrunch.com/2011/12/05/new-content-sharing-network-spling-launches-announces-400k-series-a/",
"source_description": "New Content Sharing Network Spling Launches, Announces $400K Series A",
"raised_amount": 400000.0,
"raised_currency_code": "USD",
"funded_year": 2011,
"funded_month": 12,
"funded_day": 5,
"investments":
[{"company": null,
"financial_org":
{"name": "Deep Fork Capital",
"permalink": "deep-fork-capital-2",
"image":
{"available_sizes":
[[[150,
20],
"assets/images/resized/0008/0167/80167v1-max-150x150.png"],
[[250,
34],
"assets/images/resized/0008/0167/80167v1-max-250x250.png"],
[[450,
62],
"assets/images/resized/0008/0167/80167v1-max-450x450.png"]],
"attribution": null}},
"person": null},
{"company": null,
"financial_org": null,
"person":
{"first_name": "John",
"last_name": "Ason",
"permalink": "john-ason",
"image": null}},
{"company": null,
"financial_org": null,
"person":
{"first_name": "Mitchell",
"last_name": "Blumenfeld",
"permalink": "mitchell-blumenfeld",
"image": null}},
{"company": null,
"financial_org": null,
"person":
{"first_name": "Gianni",
"last_name": "Martire",
"permalink": "gianni-martire",
"image":
{"available_sizes":
[[[138,
150],
"assets/images/resized/0006/3720/63720v4-max-150x150.jpg"],
[[230,
250],
"assets/images/resized/0006/3720/63720v4-max-250x250.jpg"],
[[414,
450],
"assets/images/resized/0006/3720/63720v4-max-450x450.jpg"]],
"attribution": ""}}}]}]
Thanks for the help!
You are using the same variable name for the counter in each loop, so when the inner loop completes and the outer loop gets to its second iteration, i is investments.length, not 1. Use different variable names for each loop:
for (var roundIdx = 0; roundIdx < fundingRounds.length; roundIdx++) {
...
for (var invIdx = 0; invIdx < investments.length; invIdx++) {
...
Also, don't populate your array using array[i] = value notation, just use array.push(value). You don't need to keep track of the indexes.
But, I'd recommend iterating your arrays using Array.forEach() and using dot syntax instead of square bracket notation:
function investorList(data, num) {
var finalInvestorList = [];
data.funding_rounds.forEach(function (round, i) {
round.investments.forEach(function (investment, i) {
if (investment.person) {
finalInvestorList.push(investment.person.first_name + " " +
investment.person.last_name);
}
else if (investment.financial_org) {
finalInvestorList.push(investment.financial_org.name)
}
else if (investment.company) {
finalInvestorList.push(investment.company.name)
}
}
}
}
For older browsers that don't natively support Array.forEach(), use the implementation here to shim the browser.

Categories

Resources