javascript - Arrays & Objects - 'substituting' value from one to another -


i have

var lookupmap. excerpt:

{ anderson: { county: 'anderson', code: 'us-tx-001' },   andrews: { county: 'andrews', code: 'us-tx-003' } , ward: { county: 'ward', code: 'us-tx-475' }, washington: { county: 'washington', code: 'us-tx-477' }, webb: { county: 'webb', code: 'us-tx-479' }} 

it long index of counties, , corresponding code. not used.

i have var uniquedata, looks like:

[ 'us-tx-477', 'us-tx-479' ] 

which array of codes.

i have var results, looks like:

{ washington: 9, webb: 4 }. 

basically, want go through elements uniquedata, , each one, county belongs to, , replace county name code, keep count same.

so, example, output should be:

{ 'us-tx-477' : 9,   'us-tx-479': 4} 

what best way this? i'm having troubles trying isolate elements...it seems confusing & can't wrap head around it.

iterate through each item in uniquedata , lookup in data object. function findinlookupmap goes through each object in lookupmap , returns county name code.

something

var cache = {};  function findinlookupmap(key){   if(cache[key]) return cache[key]; // <--- cache avoid looping in further requests   for(var o in data){      if(data[o].code == key){       cache[key] = data[o].county;       return data[o].county;     }   } }  var output = {}; for(var i=0;i<uniquedata.length;i++){ // <--- iterate through each item in uniquedata   var code = findinlookupmap(uniquedata[i]);   output[uniquedata[i]] = results[code]; } console.log(output); 

var lookupmap = {    "anderson": {      "county": 'anderson',      "code": 'us-tx-001'    },    "andrews": {      "county": 'andrews',      "code": 'us-tx-003'    },    "ward": {      "county": 'ward',      "code": 'us-tx-475'    },    "washington": {      "county": 'washington',      "code": 'us-tx-477'    },    "webb": {      "county": 'webb',      "code": 'us-tx-479'    }  };  var uniquedata = ['us-tx-477', 'us-tx-479'];    var results = {    washington: 9,    webb: 4  };  var cache = {};    function findinlookupmap(key) {    if (cache[key]) return cache[key];    (var o in lookupmap) {      if (lookupmap[o].code == key) {        cache[key] = lookupmap[o].county;        return lookupmap[o].county;      }    }  }  var output = {};  (var = 0; < uniquedata.length; i++) {    var code = findinlookupmap(uniquedata[i]);    output[uniquedata[i]] = results[code];  }  console.log(output);


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -