0

After long hours of work I figured out how to get my ajax value. It looks like this:

37,58,82,

I managed out how to remove last comma and make array like this:

Object { 0="37", 1="58", 2="82"}

But I need it to be like this (according to firebug):

[Object { trackID="track-id-37"}, Object { trackID="track-id-58"}, Object { trackID="track-id-58"},]

How can I make it to look like?

2
  • What code did you use to transform 37,58,82, into Object { 0="37", 1="58", 2="82"}? Commented Oct 21, 2013 at 21:46
  • @Nightfirecat I used tracks = $.extend({}, tracks); Commented Oct 22, 2013 at 2:56

1 Answer 1

2

Here you go. If you have your array, you can call $.map on it.

$.map(["37", "58", "82"], function(elem) { return { trackID: "track-id-" + elem }; });

Or let's say your string is called your_string...we can act directly on it (I've added whitespace to make it clear what's going on)--

your_string = '37,58,82,';
$.map(your_string.split(','), function(elem) 
                              { 
                                if (elem != '') 
                                { 
                                   return { trackID: "track-id-" + elem }; 
                                } 
                              });

The if (elem != '') statement is to protect against any empty array values after the split(','). $.map tolerates return values of undefined.

Also consider using $.trim on elem in order to build in tolerance for spaces and so on, e.g.

$.map(your_string.split(','), function(elem) 
                              { 
                                var id = $.trim(elem);
                                if (id != '') 
                                { 
                                   return { trackID: "track-id-" + id }; 
                                } 
                              });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.