Convert and modify Array in Object toa string

Te project I am working with provides me in the prebuilt drupal page with an object containing an array of FIPS codes that I am to use. To demonstrate what is being provided

var drupal={settings:{
     geo_settings:{
          primary_state_fips: "19",
          primary_county_fips: "19001", 
          compare_to: "custom", 
          custom_list: ["19003", "19005", "19007"], 
          geo: "County"}, 
     vars:{myColor: "Yellow", otherStuff: "blah blah" }
     }
}

Open in new window



I need to extract the custom_list array values and do two things with the data.  First I need to trim off the first to characters of each so in the example I am removing the 19.

Next I need to convert this data into a single string var.

So essentially want I want in this example is

theList = "003, 005, 007";

I should note that the length of the custom list value set varies from 1 - 15.

Any advice on how to make this elegant is appreciated - I can write code to accomplish is but it is not pretty so looking for simple efficient solution.

Thanks
lvmllcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
If you're using jQuery, you can do this:

var drupal={settings:{
     geo_settings:{
          primary_state_fips: "19",
          primary_county_fips: "19001", 
          compare_to: "custom", 
          custom_list: ["19003", "19005", "19007"], 
          geo: "County"}, 
     vars:{myColor: "Yellow", otherStuff: "blah blah" }
     }
}
var data = '';
$.each(drupal.settings.geo_settings.custom_list,function(index,value) {
    data += value.substr(2) + ', ';
});
if(data.length > 0) data = data.substr(0, data.length - 2);
alert(data); //data now contains "003, 005, 007"

Open in new window

Hope that helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
quizwedgeCommented:
If you're not using jQuery:

var drupal={settings:{
     geo_settings:{
          primary_state_fips: "19",
          primary_county_fips: "19001", 
          compare_to: "custom", 
          custom_list: ["19003", "19005", "19007"], 
          geo: "County"}, 
     vars:{myColor: "Yellow", otherStuff: "blah blah" }
     }
};
var custom_list = drupal.settings.geo_settings.custom_list;
var data = '';
for(var i=0; i < custom_list.length; i++){
	data += custom_list[i].substring(2) + ', ';
}
if(data.length > 0){
	data = data.substring(0, data.length - 2);
}
alert(data);

Open in new window

0
lvmllcAuthor Commented:
Thanks guys - both methods work well. I will go with the jQuery version but like having both.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.