[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert and modify Array in Object toa string

Posted on 2014-08-14
3
Medium Priority
?
190 Views
Last Modified: 2014-08-14
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
0
Comment
Question by:lvmllc
3 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 1000 total points
ID: 40260851
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
 
LVL 14

Assisted Solution

by:quizwedge
quizwedge earned 600 total points
ID: 40260956
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
 

Author Closing Comment

by:lvmllc
ID: 40261364
Thanks guys - both methods work well. I will go with the jQuery version but like having both.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question