Object in javascript stuck on last line of csv file - javascript

Im currently trying to build a choropleth on the map of Puerto Rico using D3. Using Scott Murray's Example on Choropleths, I'm trying to bind data to each municipality of Puerto Rico. I have a placeholder empty object to hold the data being fed from the CSV like so:
var properties = {};
Then, I proceed to read the CSV file by row. I assign the corresponding values to some variables like so:
properties.dataID = parseFloat(data[i].id);
properties.dataMunic_Total = parseFloat(data[i].municipio_total);
//etc.
All fine and dandy. I console.log to check whether or not the data was stored correctly:
console.log(properties.dataID);
//Displays 1, which is correct.
However, when I try to access the whole object:
console.log(properties);
it only displays the last row of the CSV file. I ran that last console.log() statement inside the first for loop, immediately after assigning the data to the object's properties, and every time the loop iterates through the console.log(), it ONLY displays the last line of the CSV file.
When I try to assign the object properties to my map's objects (municipalities), all that it ever assigns is that last line of the CSV file. So I tried setting them up individually, seeing that seems to have worked. However, JavaScript complains that the property does not exist in the object:
obj.geometries[j].properties.year[properties.dataYear - 2000].id = properties.dataID;
Uncaught TypeError: Cannot read property 'id' of undefined
I tried initializing it as if it were a new variable:
var obj.geometries[j].properties.year[properties.dataYear - 2000].id = properties.dataID;
but then I got this back on the console:
Uncaught SyntaxError: Unexpected identifier
Aside from the syntax error, I think that this is mainly due to the fact that I appended an array to each municipality so I could hold different sets of data per year... but I don't understand why JavaScript behaves this way.
My question is: why does my object behave this way? Is there something wrong with d3.csv? Am I doing something wrong?

If you want to store several properties, you need to use an array (i.e. a list). In the code you are using, you are only creating a single variable properties at a time and not saving the references to the ones you have created previously. That is, in each iteration of the loop, you are creating a new object, but losing the old one.
If you declare properties as an array and then append new property objects in the loop, you can reference them later and use them to draw the map. The code would look something like this.
var properties = [];
for(var i = 0; i < data.length; i++) {
var property = {};
property.dataID = parseFloat(data[i].id);
property.dataMunic_Total = parseFloat(data[i].municipio_total);
// ...
properties.push(property);
}
After setting up the data like this, you should be able to proceed as described in the example.

Related

How is data stored in arrays from getValue

In Google Sheets looking as part of a script to store data in a range to an array then to check whether a certain column has an "Y" in so I can loop through and store these columns in new arrays.
I have the following code but am getting this error - "TypeError: Cannot read property "0.0" from undefined."
var data = sheet.getRange("A6:U37").getValues;
if (data[20][i]=="Y"){
(The if code is generating the error)
Believe I am misunderstanding how the range is stored in the array causing the error any advice?
In the first line of code you provided, you are referencing the function getValues rather than actually calling it. In order to do so, you just have to modify the code as follows:
var data = sheet.getRange("A6:U37").getValues();
if (data[20][i]=="Y"){
Next time you have issues similar to this one, you can consider using logging or other debugging techniques in order to debug your script.

Object.assign not working as expected

I have one object called bookings, and inside it I have several properties, and i want extend with Object.assign, like this:
let data = Object.assign(booking, {
hiw: event.hiw[booking.locale],
tip: event.tip[booking.locale],
start: moment(event.start).format('L')
});
But when I print the data, the result will be the same object from the source (booking), so hiw, tip and start will be ignored, but... if I try to do:
let data = Object.assign({}, {
hiw: event.hiw[booking.locale],
tip: event.tip[booking.locale],
start: moment(event.start).format('L')
});
This will work perfect.
My question is: what am I doing wrong here? Why can't I extend booking and I can extend the empty object?
That's definitely not a problem with async code, when i try to extend booking, he already exist with all properties inside.
I also was trying to use underscore extend method, and the behavior is exactly the same.
Mongoose documents (model instances) are special: they will only print properties that are present in the schema.
If properties aren't defined there, they won't show up when you console.log() them (and also not if you convert the document to a plain JS object with obj.toObject()).
This means that using Object.assign() will only work if you assign properties that are also present in the schema. Any properties that aren't declared will not be shown (nor saved to the database).
If your intention is to use the document for output, you should convert it to a proper JS object first before assigning to it:
let data = Object.assign(booking.toObject(), {
hiw : event.hiw[booking.locale],
tip : event.tip[booking.locale],
start : moment(event.start).format('L')
});

jQuery Id Selection and Dynamic Arrays

I have some code I'm struggling with. The good news is the code working as intended for a single instance; after some thought I've decided to feature multiple of these image selectors on a page. This works but the ugly approach of duplicating the code doesn't scale well (e.g. what if you want 50 of these on there?) The snag I've hit is how I can refer to a specific array. Is an array even an ideal solution for this?
The Objective
I have a series of images that a user may select from, up to X amount. The selected image ids are stored in an array and the image is added to a "selected images pool". This occurs by using an onClick for the slider, I obtain the Id from the element attributes. This is where I'm getting stuck.
var dataArray = $(this).closest("[id^=carousel]").data('array');
var slideCounter = $(this).closest("[id^=carousel]").data('counter');
slideCounter = dataArray.length;
The slideCounter returns the length of the string, not the array elements. How can I tell this code to refer to a particular array? See the fiddle for a better idea of the markup and code: jsFiddle
I have no doubt that there is a better approach. I'm relatively new to front end work, I'd appreciate any insights, I've burnt some brain cells on this, thanks!
From looking at your HTML, it looks like when you do this:
var dataArray = $(this).closest("[id^=carousel]").data('array');
what you're trying to do is to read the name of an array with .data() and then somehow turn that name (which is a string) into the array that's in your variable. My guess is that there's probably a better way to structure your code rather than putting javascript variable names in your HTML. I'd probably put a key name in the HTML and then store the arrays in an object where you can access them by that key name at any time.
Without trying to refactor your code, here's an idea for what you were trying to accomplish:
If selectedSlidesIdArray1 is a global variable, then you can do this:
var dataArray = window[$(this).closest("[id^=carousel]").data('array')];
Using the [stringVariable] notation on an object, lets you access a property by a literal string or a variable that contains a string. Since all global variables are also properties on the window object, you can do it this way for global variables.
If selectedSlidesIdArray1 is not a global variable, then you should probably put it in an object and then you can do this:
var dataArray = yourObj[$(this).closest("[id^=carousel]").data('array')];
Instead of trying to translate an arbitrary string into a JavaScript variable of the same name, why not just use another array? You can have nested arrays, which is to say an array of arrays.
Thus, instead of selectedSlidesIdArray1, selectedSlidesIdArray2, etc., you would have one selectedSlidesIdArray with sub-arrays, which you could then pull the index for using a data attribute.

How to get value in JS object?

I just give an example of a script that suddenly stopped working. The problem is that i cant get any values from this object, although in the past there were no problems.
var points = '{"points":{"point":[{"lat":"55.68504841066897","lon":"21.128827091306448"},{"lat":"55.68509585224092","lon":"21.128621818497777"},{"lat":"55.68510725162923","lon":"21.128564486280084"},{"lat":"55.685256784781814","lon":"21.128428364172578"},{"lat":"55.68540941923857","lon":"21.12827841192484"},{"lat":"55.68553472869098","lon":"21.12819341942668"},{"lat":"55.68563086912036","lon":"21.128090489655733"},{"lat":"55.685754250735044","lon":"21.128079341724515"},{"lat":"55.685868579894304","lon":"21.12806241028011"},{"lat":"55.686048371717334","lon":"21.127971885725856"},{"lat":"55.68620461039245","lon":"21.12780307419598"},{"lat":"55.686277113854885","lon":"21.127677345648408"},{"lat":"55.68637593649328","lon":"21.12757139839232"},{"lat":"55.68648599088192","lon":"21.127493027597666"},{"lat":"55.68663602694869","lon":"21.127418177202344"},{"lat":"55.68679201416671","lon":"21.12734558992088"},{"lat":"55.686920089647174","lon":"21.12726939842105"},{"lat":"55.68693928420544","lon":"21.127255652099848"},{"lat":"55.68705772049725","lon":"21.127218855544925"},{"lat":"55.68714472465217","lon":"21.127141742035747"},{"lat":"55.6871934235096","lon":"21.12712439149618"},{"lat":"55.68733625113964","lon":"21.127151465043426"},{"lat":"55.68751168437302","lon":"21.12717761658132"},{"lat":"55.687646800652146","lon":"21.127120200544596"},{"lat":"55.68781033158302","lon":"21.127034788951278"},{"lat":"55.687981490045786","lon":"21.12691568210721"},{"lat":"55.68808760493994","lon":"21.126865474507213"},{"lat":"55.68817662075162","lon":"21.126789785921574"},{"lat":"55.68830654025078","lon":"21.12668802961707"},{"lat":"55.688458336517215","lon":"21.126597672700882"},{"lat":"55.68859001621604","lon":"21.126522067934275"},{"lat":"55.68873468786478","lon":"21.126421485096216"},{"lat":"55.689044734463096","lon":"21.126208417117596"},{"lat":"55.68912017159164","lon":"21.126169441267848"},{"lat":"55.68925394676626","lon":"21.1261201556772"},{"lat":"55.68945947103202","lon":"21.12605821341276"},{"lat":"55.68962057121098","lon":"21.125976908951998"},{"lat":"55.68978569470346","lon":"21.125826286152005"},{"lat":"55.68990069441497","lon":"21.125754453241825"},{"lat":"55.68995366804302","lon":"21.125659067183733"},{"lat":"55.69000102579594","lon":"21.1256090272218"},{"lat":"55.69016573019326","lon":"21.1255088634789"},{"lat":"55.690477369353175","lon":"21.12531390041113"},{"lat":"55.69060888141394","lon":"21.125247851014137"},{"lat":"55.69067928940058","lon":"21.12515070475638"},{"lat":"55.6907766032964","lon":"21.124989185482264"},{"lat":"55.69090216420591","lon":"21.1248526442796"},{"lat":"55.69097299128771","lon":"21.124745020642877"},{"lat":"55.691056391224265","lon":"21.124557433649898"},{"lat":"55.69115781225264","lon":"21.124385772272944"},{"lat":"55.6912134680897","lon":"21.124245207756758"},{"lat":"55.691240122541785","lon":"21.124201118946075"},{"lat":"55.69127884693444","lon":"21.124075977131724"},{"lat":"55.69134992547333","lon":"21.12395158968866"},{"lat":"55.69142796099186","lon":"21.123761823400855"},{"lat":"55.691470289602876","lon":"21.123608015477657"},{"lat":"55.691630467772484","lon":"21.123296124860644"},{"lat":"55.69177580997348","lon":"21.123084481805563"},{"lat":"55.691883098334074","lon":"21.12299303524196"},{"lat":"55.691998517140746","lon":"21.122908294200897"},{"lat":"55.692141177132726","lon":"21.122815422713757"},{"lat":"55.6923047080636","lon":"21.12273939885199"},{"lat":"55.692533031105995","lon":"21.122592883184552"},{"lat":"55.69262858480215","lon":"21.122528091073036"},{"lat":"55.692712320014834","lon":"21.1224711779505"},{"lat":"55.69280887953937","lon":"21.122405463829637"},{"lat":"55.69282790645957","lon":"21.12241141498089"},{"lat":"55.69290057756007","lon":"21.122407307848334"},{"lat":"55.69293066859245","lon":"21.12237486988306"},{"lat":"55.69301373325288","lon":"21.122297504916787"},{"lat":"55.69308330304921","lon":"21.122258696705103"},{"lat":"55.69318933412433","lon":"21.121885618194938"},{"lat":"55.693208780139685","lon":"21.121837757527828"},{"lat":"55.69326653145254","lon":"21.121622258797288"},{"lat":"55.693393936380744","lon":"21.121310954913497"},{"lat":"55.693441880866885","lon":"21.121110962703824"},{"lat":"55.69349527359009","lon":"21.120854392647743"},{"lat":"55.69358579814434","lon":"21.120616095140576"},{"lat":"55.69369417615235","lon":"21.12032457254827"},{"lat":"55.69377011619508","lon":"21.120052495971322"},{"lat":"55.693774642422795","lon":"21.120002456009388"},{"lat":"55.693801464512944","lon":"21.11992777325213"},{"lat":"55.693983267992735","lon":"21.119664330035448"},{"lat":"55.69409340620041","lon":"21.119460901245475"},{"lat":"55.69415652193129","lon":"21.11929250881076"},{"lat":"55.69417831487954","lon":"21.119207181036472"},{"lat":"55.69424897432327","lon":"21.118967961519957"},{"lat":"55.694260792806745","lon":"21.118934266269207"},{"lat":"55.69426699541509","lon":"21.118877101689577"},{"lat":"55.69431468844414","lon":"21.11856361851096"},{"lat":"55.69434058852494","lon":"21.118531934916973"},{"lat":"55.69443111307919","lon":"21.118354992941022"},{"lat":"55.69457972422242","lon":"21.1182370595634"},{"lat":"55.69472850300372","lon":"21.118099931627512"},{"lat":"55.694737220183015","lon":"21.118061039596796"},{"lat":"55.69480947218835","lon":"21.117927934974432"},{"lat":"55.69499395787716","lon":"21.117815114557743"},{"lat":"55.69512295536697","lon":"21.11766935326159"},{"lat":"55.69523921236396","lon":"21.11754127778113"},{"lat":"55.695358319208026","lon":"21.117476485669613"},{"lat":"55.695498967543244","lon":"21.117387553676963"},{"lat":"55.69560918956995","lon":"21.11733608879149"},{"lat":"55.69572452455759","lon":"21.117292754352093"},{"lat":"55.695821000263095","lon":"21.11724623478949"},{"lat":"55.695909429341555","lon":"21.117141377180815"},{"lat":"55.696009173989296","lon":"21.117025455459952"},{"lat":"55.69604102522135","lon":"21.117006093263626"},{"lat":"55.696090226992965","lon":"21.116960495710373"},{"lat":"55.69625677540898","lon":"21.116869132965803"},{"lat":"55.69637831300497","lon":"21.116807106882334"},{"lat":"55.69654083810747","lon":"21.11673066392541"},{"lat":"55.696675116196275","lon":"21.116644330322742"},{"lat":"55.69678684696555","lon":"21.116551458835602"},{"lat":"55.69688340649009","lon":"21.116469986736774"},{"lat":"55.697014667093754","lon":"21.116367476060987"},{"lat":"55.69704534485936","lon":"21.11634660512209"},{"lat":"55.69713561795652","lon":"21.116271084174514"},{"lat":"55.69718171842396","lon":"21.116163125261664"},{"lat":"55.69733996875584","lon":"21.116052316501737"},{"lat":"55.697473073378205","lon":"21.11595081165433"},{"lat":"55.697658313438296","lon":"21.115931114181876"},{"lat":"55.69781731814146","lon":"21.115811336785555"},{"lat":"55.69795947521925","lon":"21.115705221891403"},{"lat":"55.698050586506724","lon":"21.115594832226634"},{"lat":"55.69814337417483","lon":"21.115462817251682"},{"lat":"55.69816265255213","lon":"21.115436078980565"},{"lat":"55.69827714934945","lon":"21.11534840427339"},{"lat":"55.69821269251406","lon":"21.115194596350193"},{"lat":"55.69802870973945","lon":"21.11485881730914"},{"lat":"55.69793617352843","lon":"21.114696208387613"},{"lat":"55.69779242388904","lon":"21.114436956122518"},{"lat":"55.697687985375524","lon":"21.114247357472777"},{"lat":"55.69759452715516","lon":"21.11412481404841"},{"lat":"55.697459829971194","lon":"21.113973017781973"},{"lat":"55.69730476476252","lon":"21.11382189206779"},{"lat":"55.69720124825835","lon":"21.113727260380983"},{"lat":"55.697076106444","lon":"21.113614523783326"},{"lat":"55.6967859249562","lon":"21.11329223960638"},{"lat":"55.69665038958192","lon":"21.113007087260485"},{"lat":"55.69655232131481","lon":"21.112801395356655"},{"lat":"55.69646816700697","lon":"21.112644569948316"},{"lat":"55.696425419300795","lon":"21.11255052499473"},{"lat":"55.69636255502701","lon":"21.112362518906593"},{"lat":"55.69635140709579","lon":"21.112339636310935"},{"lat":"55.69623523391783","lon":"21.112123718485236"},{"lat":"55.696120569482446","lon":"21.111881816759706"},{"lat":"55.696040857583284","lon":"21.11167528666556"},{"lat":"55.69595008157194","lon":"21.11142022535205"},{"lat":"55.695875734090805","lon":"21.11120472662151"},{"lat":"55.69572854787111","lon":"21.110760234296322"},{"lat":"55.69564430974424","lon":"21.11053735949099"},{"lat":"55.69553685374558","lon":"21.11026863567531"},{"lat":"55.69546644575894","lon":"21.110082305967808"},{"lat":"55.695343650877476","lon":"21.109795225784183"},{"lat":"55.69524306803942","lon":"21.1095180362463"},{"lat":"55.69515866227448","lon":"21.109181251376867"},{"lat":"55.695100324228406","lon":"21.108942283317447"},{"lat":"55.69498088210821","lon":"21.1084093619138"},{"lat":"55.69490510970354","lon":"21.108084814623"},{"lat":"55.69484299980104","lon":"21.107756663113832"},{"lat":"55.69478918798268","lon":"21.10747955739498"},{"lat":"55.694745602086186","lon":"21.10724243335426"},{"lat":"55.6946914549917","lon":"21.106920819729567"},{"lat":"55.69466907531023","lon":"21.10674454830587"},{"lat":"55.69461132399738","lon":"21.106461994349957"},{"lat":"55.6945738568902","lon":"21.106215063482523"},{"lat":"55.69453219883144","lon":"21.10587878152728"},{"lat":"55.69449230097234","lon":"21.1055772844702"},{"lat":"55.694455252960324","lon":"21.105288863182068"},{"lat":"55.69440764375031","lon":"21.104955095797777"},{"lat":"55.69435643032193","lon":"21.10461462289095"},{"lat":"55.694305300712585","lon":"21.10427993349731"},{"lat":"55.69425861351192","lon":"21.103936610743403"},{"lat":"55.69421645253897","lon":"21.10363251529634"},{"lat":"55.69416725076735","lon":"21.10329195857048"},{"lat":"55.6941232457757","lon":"21.102952240034938"},{"lat":"55.69407915696502","lon":"21.10261511988938"},{"lat":"55.694058453664184","lon":"21.102374475449324"},{"lat":"55.69406448863447","lon":"21.102328961715102"},{"lat":"55.69408108480275","lon":"21.102257715538144"},{"lat":"55.694214943796396","lon":"21.10212218016386"},{"lat":"55.69423698820174","lon":"21.10211681574583"},{"lat":"55.69442750886083","lon":"21.102087311446667"},{"lat":"55.6946084741503","lon":"21.102057807147503"},{"lat":"55.69477544166148","lon":"21.10203324817121"},{"lat":"55.69497543387115","lon":"21.102013969793916"},{"lat":"55.695172073319554","lon":"21.101996451616287"},{"lat":"55.695517072454095","lon":"21.1019354313612"},{"lat":"55.69569854065776","lon":"21.101907016709447"},{"lat":"55.69586425088346","lon":"21.101873824372888"},{"lat":"55.6960675958544","lon":"21.101846415549517"},{"lat":"55.69624001160264","lon":"21.10184323042631"},{"lat":"55.69641628302634","lon":"21.10182219184935"},{"lat":"55.696614012122154","lon":"21.101797549054027"},{"lat":"55.696769915521145","lon":"21.101831747218966"},{"lat":"55.696862032637","lon":"21.101882876828313"},{"lat":"55.69701533764601","lon":"21.10196049325168"},{"lat":"55.69717912003398","lon":"21.102033918723464"},{"lat":"55.69730317220092","lon":"21.102072978392243"},{"lat":"55.697391014546156","lon":"21.102091083303094"},{"lat":"55.69750685244799","lon":"21.102119330316782"},{"lat":"55.69768438115716","lon":"21.10213140025735"},{"lat":"55.69789099507034","lon":"21.102197282016277"},{"lat":"55.69806341081858","lon":"21.102357544004917"},{"lat":"55.698223086073995","lon":"21.10252090729773"},{"lat":"55.69837689399719","lon":"21.102673541754484"},{"lat":"55.69846322759986","lon":"21.102789379656315"},{"lat":"55.69862592034042","lon":"21.102938996627927"},{"lat":"55.69877210073173","lon":"21.10307989642024"},{"lat":"55.698943845927715","lon":"21.103275194764137"},{"lat":"55.69907653145492","lon":"21.103442329913378"},{"lat":"55.69917334243655","lon":"21.10357333905995"},{"lat":"55.69931994192302","lon":"21.10376344062388"},{"lat":"55.6994594167918","lon":"21.103932755067945"},{"lat":"55.69959998130798","lon":"21.104121347889304"},{"lat":"55.69972101598978","lon":"21.104257218539715"},{"lat":"55.69983718916774","lon":"21.104403315111995"},{"lat":"55.699925953522325","lon":"21.104517225176096"},{"lat":"55.70000440813601","lon":"21.10463515855372"},{"lat":"55.700119407847524","lon":"21.104831881821156"},{"lat":"55.700194761157036","lon":"21.104944869875908"},{"lat":"55.700298361480236","lon":"21.105100521817803"},{"lat":"55.70035099983215","lon":"21.105193058028817"},{"lat":"55.700479159131646","lon":"21.105334293097258"},{"lat":"55.70057806558907","lon":"21.10552154481411"},{"lat":"55.70067437365651","lon":"21.105648279190063"},{"lat":"55.70073002949357","lon":"21.105717681348324"},{"lat":"55.7008218113333","lon":"21.10586704686284"},{"lat":"55.70086129009724","lon":"21.10593887977302"},{"lat":"55.70092448964715","lon":"21.106116073206067"},{"lat":"55.700939912348986","lon":"21.106153121218085"},{"lat":"55.70101895369589","lon":"21.106260158121586"},{"lat":"55.70112104527652","lon":"21.10640600323677"},{"lat":"55.701140239834785","lon":"21.106439866125584"},{"lat":"55.70122975856066","lon":"21.10659702681005"},{"lat":"55.70134676992893","lon":"21.106788301840425"},{"lat":"55.70149462670088","lon":"21.107030119746923"},{"lat":"55.7015554793179","lon":"21.107191806659102"},{"lat":"55.70156687870622","lon":"21.10722776502371"},{"lat":"55.70158204995096","lon":"21.10726405866444"},{"lat":"55.70164759643376","lon":"21.107506547123194"},{"lat":"55.7017439045012","lon":"21.10772917047143"},{"lat":"55.701823867857456","lon":"21.107928659766912"},{"lat":"55.701906429603696","lon":"21.108162263408303"},{"lat":"55.701991338282824","lon":"21.108396286144853"},{"lat":"55.702030481770635","lon":"21.10861010849476"},{"lat":"55.702101895585656","lon":"21.108935242518783"},{"lat":"55.70214254781604","lon":"21.10925668850541"},{"lat":"55.702180517837405","lon":"21.109491800889373"},{"lat":"55.70218571461737","lon":"21.109543684870005"},{"lat":"55.70222376845777","lon":"21.109786089509726"},{"lat":"55.702235251665115","lon":"21.109872004017234"},{"lat":"55.702300211414695","lon":"21.11002128571272"},{"lat":"55.70232384838164","lon":"21.110096806660295"},{"lat":"55.702370032668114","lon":"21.110205352306366"},{"lat":"55.702406242489815","lon":"21.11027291044593"},{"lat":"55.70246893912554","lon":"21.110466783866286"},{"lat":"55.70252032019198","lon":"21.11060667783022"},{"lat":"55.70260698907077","lon":"21.11077280715108"},{"lat":"55.70267706178129","lon":"21.110882526263595"},{"lat":"55.70276850834489","lon":"21.111083775758743"},{"lat":"55.702775130048394","lon":"21.111104981973767"},{"lat":"55.70286297239363","lon":"21.11128494143486"},{"lat":"55.70300118997693","lon":"21.111558778211474"},{"lat":"55.70306338369846","lon":"21.111747287213802"},{"lat":"55.70311635732651","lon":"21.111963791772723"},{"lat":"55.70317846722901","lon":"21.1121497862041"},{"lat":"55.70320604369044","lon":"21.112229246646166"},{"lat":"55.70331039838493","lon":"21.112473662942648"},{"lat":"55.70332858711481","lon":"21.112521775066853"},{"lat":"55.703394301235676","lon":"21.112708607688546"},{"lat":"55.70343998260796","lon":"21.112926369532943"},{"lat":"55.70347133092582","lon":"21.113010859116912"},{"lat":"55.703507205471396","lon":"21.113134576007724"},{"lat":"55.70362228900194","lon":"21.113395588472486"},{"lat":"55.70369663648307","lon":"21.11357068642974"},{"lat":"55.70374617353082","lon":"21.11371041275561"},{"lat":"55.703856060281396","lon":"21.11395927146077"},{"lat":"55.70397935807705","lon":"21.114195305854082"},{"lat":"55.704098884016275","lon":"21.11435799859464"},{"lat":"55.70410215295851","lon":"21.11437937244773"},{"lat":"55.70409829728305","lon":"21.114418348297477"},{"lat":"55.703970389440656","lon":"21.114557068794966"},{"lat":"55.70385723374784","lon":"21.114660082384944"},{"lat":"55.703788083046675","lon":"21.11475228331983"},{"lat":"55.70375807583332","lon":"21.11479042097926"},{"lat":"55.70371499285102","lon":"21.114920508116484"},{"lat":"55.70367744192481","lon":"21.115069873631"},{"lat":"55.70365657098591","lon":"21.115141035988927"},{"lat":"55.70362321101129","lon":"21.115276655182242"},{"lat":"55.70356797426939","lon":"21.115418979898095"},{"lat":"55.70342883467674","lon":"21.11560095101595"},{"lat":"55.70326941087842","lon":"21.115726176649332"},{"lat":"55.70318986661732","lon":"21.11577990464866"},{"lat":"55.703066401183605","lon":"21.11588199622929"},{"lat":"55.70296254940331","lon":"21.115961372852325"},{"lat":"55.70288476534188","lon":"21.116036139428616"},{"lat":"55.702633475884795","lon":"21.11627426929772"},{"lat":"55.70257036015391","lon":"21.116339899599552"},{"lat":"55.70243281312287","lon":"21.116461688652635"},{"lat":"55.70231202989817","lon":"21.116568725556135"},{"lat":"55.7021548692137","lon":"21.116699567064643"},{"lat":"55.70210105739534","lon":"21.11675119958818"},{"lat":"55.702026626095176","lon":"21.11681615933776"},{"lat":"55.70182361640036","lon":"21.1170213483274"},{"lat":"55.701650362461805","lon":"21.117181023582816"},{"lat":"55.701496470719576","lon":"21.117324270308018"},{"lat":"55.70132212713361","lon":"21.117485286667943"},{"lat":"55.70119564421475","lon":"21.117601543664932"},{"lat":"55.70102406665683","lon":"21.117759877815843"},{"lat":"55.70085408166051","lon":"21.117912847548723"},{"lat":"55.7006854377687","lon":"21.118055507540703"},{"lat":"55.70051050744951","lon":"21.118215937167406"},{"lat":"55.70034110918641","lon":"21.11837100237608"},{"lat":"55.70016961544752","lon":"21.11850938759744"},{"lat":"55.70001899264753","lon":"21.11847502179444"},{"lat":"55.69988773204386","lon":"21.118254913017154"},{"lat":"55.699858982115984","lon":"21.118093393743038"},{"lat":"55.699802823364735","lon":"21.11786289140582"},{"lat":"55.69972487166524","lon":"21.117673041298985"},{"lat":"55.69963292218745","lon":"21.117400294169784"},{"lat":"55.69957634434104","lon":"21.117237601429224"},{"lat":"55.69949244149029","lon":"21.117088235914707"},{"lat":"55.69936495274305","lon":"21.116900900378823"},{"lat":"55.69922732189298","lon":"21.116730496287346"},{"lat":"55.69912137463689","lon":"21.11659931950271"},{"lat":"55.698979469016194","lon":"21.11641307361424"},{"lat":"55.69888559170067","lon":"21.116287261247635"},{"lat":"55.69878492504358","lon":"21.11615516245365"},{"lat":"55.698687359690666","lon":"21.116036558523774"},{"lat":"55.69857311435044","lon":"21.115854252129793"},{"lat":"55.69844554178417","lon":"21.115700444206595"},{"lat":"55.69828896783292","lon":"21.11554043367505"},{"lat":"55.698210345581174","lon":"21.115515874698758"},{"lat":"55.69815058261156","lon":"21.115539092570543"},{"lat":"55.69802971556783","lon":"21.115668760612607"},{"lat":"55.697909854352474","lon":"21.115807816386223"},{"lat":"55.69778932258487","lon":"21.11594687215984"},{"lat":"55.6977547891438","lon":"21.11598316580057"},{"lat":"55.69764833897352","lon":"21.1160128377378"},{"lat":"55.69745027460158","lon":"21.116042342036963"},{"lat":"55.69733301177621","lon":"21.116149881854653"},{"lat":"55.69723980501294","lon":"21.116232946515083"},{"lat":"55.6972227897495","lon":"21.116261025890708"},{"lat":"55.69715363904834","lon":"21.116349454969168"},{"lat":"55.69708792492747","lon":"21.116425143554807"},{"lat":"55.69701340980828","lon":"21.11648985184729"},{"lat":"55.69685759022832","lon":"21.11659051850438"},{"lat":"55.696704955771565","lon":"21.11672094091773"},{"lat":"55.696525666862726","lon":"21.116858907043934"},{"lat":"55.69639013148844","lon":"21.116948761045933"},{"lat":"55.696241771802306","lon":"21.11704431474209"},{"lat":"55.69608804769814","lon":"21.117151686921716"},{"lat":"55.695942621678114","lon":"21.117259142920375"},{"lat":"55.69578764028847","lon":"21.117350086569786"},{"lat":"55.69563358090818","lon":"21.11743139103055"},{"lat":"55.69551539607346","lon":"21.11752233467996"},{"lat":"55.69535999558866","lon":"21.117640854790807"},{"lat":"55.69524130783975","lon":"21.11772115342319"},{"lat":"55.69511708803475","lon":"21.117845540866256"},{"lat":"55.694992281496525","lon":"21.117994571104646"},{"lat":"55.694970237091184","lon":"21.118015022948384"},{"lat":"55.694850040599704","lon":"21.11820629797876"},{"lat":"55.694762114435434","lon":"21.118307802826166"},{"lat":"55.69462230429053","lon":"21.118422215804458"},{"lat":"55.69453161209822","lon":"21.118590272963047"},{"lat":"55.69442725740373","lon":"21.11873108893633"},{"lat":"55.6943547539413","lon":"21.118875006213784"},{"lat":"55.69434025324881","lon":"21.118914736434817"},{"lat":"55.694283256307244","lon":"21.119155883789062"},{"lat":"55.694209076464176","lon":"21.11935520544648"},{"lat":"55.69414118304849","lon":"21.11953985877335"},{"lat":"55.69409332238138","lon":"21.119693918153644"},{"lat":"55.69401637651026","lon":"21.119905896484852"},{"lat":"55.69391302764416","lon":"21.12019557505846"},{"lat":"55.69383021444082","lon":"21.120406966656446"},{"lat":"55.69374010898173","lon":"21.120611736550927"},{"lat":"55.69366995245218","lon":"21.12077794969082"},{"lat":"55.69358906708658","lon":"21.120989341288805"},{"lat":"55.69350776262581","lon":"21.121180281043053"},{"lat":"55.693437019363046","lon":"21.12135705538094"},{"lat":"55.693372059613466","lon":"21.12154170870781"},{"lat":"55.6933462433517","lon":"21.121611446142197"},{"lat":"55.693264938890934","lon":"21.121894922107458"},{"lat":"55.69322261027992","lon":"21.12200832925737"},{"lat":"55.693137031048536","lon":"21.122179068624973"},{"lat":"55.69298422895372","lon":"21.122334888204932"},{"lat":"55.69281131029129","lon":"21.12247746437788"},{"lat":"55.69267409853637","lon":"21.122561115771532"},{"lat":"55.692514926195145","lon":"21.122676534578204"},{"lat":"55.69232985377312","lon":"21.1228184401989"},{"lat":"55.692206136882305","lon":"21.122904773801565"},{"lat":"55.692184595391154","lon":"21.122915083542466"},{"lat":"55.692057609558105","lon":"21.12300770357251"},{"lat":"55.69194286130369","lon":"21.123074255883694"},{"lat":"55.69183398038149","lon":"21.12316000275314"},{"lat":"55.69173591211438","lon":"21.12329293973744"},{"lat":"55.6916831061244","lon":"21.12344096414745"},{"lat":"55.69161462597549","lon":"21.123603070154786"},{"lat":"55.69151639007032","lon":"21.123859137296677"},{"lat":"55.69143156521022","lon":"21.12404546700418"},{"lat":"55.6913479976356","lon":"21.124228527769446"},{"lat":"55.69128379225731","lon":"21.124351155012846"},{"lat":"55.69121061824262","lon":"21.124528096988797"},{"lat":"55.6911252066493","lon":"21.124728843569756"},{"lat":"55.69101808592677","lon":"21.124975690618157"},{"lat":"55.690926387906075","lon":"21.125151123851538"},{"lat":"55.690736873075366","lon":"21.125306021422148"},{"lat":"55.69059781730175","lon":"21.12537802197039"},{"lat":"55.69040486589074","lon":"21.125512551516294"},{"lat":"55.69023320451379","lon":"21.12565152347088"},{"lat":"55.69006850011647","lon":"21.125793596729636"},{"lat":"55.6899353954941","lon":"21.125906417146325"},{"lat":"55.689752670004964","lon":"21.12604052759707"},{"lat":"55.68956717848778","lon":"21.126177571713924"},{"lat":"55.689404821023345","lon":"21.126235406845808"},{"lat":"55.68924145773053","lon":"21.12626734189689"},{"lat":"55.6890619173646","lon":"21.126314364373684"},{"lat":"55.68879344500601","lon":"21.12650128081441"},{"lat":"55.688646510243416","lon":"21.126614101231098"},{"lat":"55.68847116082907","lon":"21.126740835607052"},{"lat":"55.688291201367974","lon":"21.126871844753623"},{"lat":"55.688115768134594","lon":"21.127001428976655"},{"lat":"55.68796212784946","lon":"21.127109890803695"},{"lat":"55.687809493392706","lon":"21.127204187214375"},{"lat":"55.68767613731325","lon":"21.12729043699801"},{"lat":"55.68748771212995","lon":"21.127318432554603"},{"lat":"55.68730900995433","lon":"21.127259843051434"},{"lat":"55.68715847097337","lon":"21.127320528030396"},{"lat":"55.6869635079056","lon":"21.12742287106812"},{"lat":"55.68678547628224","lon":"21.127511132508516"},{"lat":"55.686605935916305","lon":"21.127605764195323"},{"lat":"55.6864420697093","lon":"21.127705005928874"},{"lat":"55.68629245273769","lon":"21.127832075580955"},{"lat":"55.68617376498878","lon":"21.127942381426692"},{"lat":"55.6860120780766","lon":"21.128108175471425"},{"lat":"55.68582398816943","lon":"21.12822954542935"},{"lat":"55.68569775670767","lon":"21.128230299800634"},{"lat":"55.68557999096811","lon":"21.12821345217526"},{"lat":"55.685403468087316","lon":"21.128289978951216"},{"lat":"55.68525896407664","lon":"21.128417551517487"},{"lat":"55.68512233905494","lon":"21.12855593673885"},{"lat":"55.68504153750837","lon":"21.12874218262732"},{"lat":"55.68503189831972","lon":"21.12878618761897"},{"lat":"55.68502544425428","lon":"21.12883354537189"}]}}';
points = JSON.parse(points);
console.log(points); // Here is my full object, i can navigate it and also copy full path
console.log(points.point[0]); // produces TypeError: points.point is undefined
Isnt it points.points.point[0]? The first 'points' is the variable, not the content.
With another varname it might be a bit more clear:
pointsJSON = JSON.parse(pointsJSON);
pointsJSON.points.point[0]
As points is your object name and with same name your are having property of the object so you can get
points.points.point[0]
First points is variable name, second one is first property of object and third one is the inner property

Undefined values in Javascript object

So I am trying to store an array of objects into localStorage, as follows:-
EDIT: The following is part of a function that is called in a loop.
c = [{"name":nameDOM.value,"add":+addDOM.value,"town":townDOM.value,"post":postalDOM.value,"mob":mobDOM.value}];
cData = cData.concat(c);
localStorage.setItem('cData', cData);
However, after a page refresh, when I try to access data from the objects, it is apparently undefined. Accessing data from the objects is fine before a refresh.
I am accessing the data in the following manner:-
//Table code omitted.
var text = document.createTextNode(""+cData[i].name+", "+cData[i].add+", "+cData[i].town+", "+cData[i].post+", "+cData[i].mob+"");
I have been trying to debug the problem using Chromes Javascript tools, as well as inserting alerts into various places to monitor the state of the variables; still undefined.
You've made an oopsies. Try:
c = [{"name":nameDOM.value,"add":+addDOM.value,"town":townDOM.value,"post":postalDOM.value,"mob":mobDOM.value}];
cData = cData.concat(c);
localStorage.setItem('cData', JSON.stringify(cData));
They difference being that you are turning your array of objects into a json string that can be parsed later by your code using:
eval(localStorage.getItem('cData'));

Categories

Resources