0

Let's say I receive some data from jQuery Ajax output.

var tags = call_ajax('order/get_data', 'user_id=' + $('#user_id').val());

Outputted string from PHP file something like this :

echo "Aldi:01|Andi:02|Martin:03";

So my question is, how to .split() that outputted string(tags) become JS array with format like :

var source: [
                {'value' : 'Aldi', 'id' : '01'},
                {'value' : 'Andi', 'id' : '02'},
                {'value' : 'Martin', 'id' : '03'}
            ]

Thanks in advance!

1
  • What exactly does call_ajax do? Does it return the response synchronously, or the request promise? Commented Aug 1, 2013 at 1:46

4 Answers 4

2

With the Array map method:

var source = tags.split("|") // first split by `|`
  .map(function(tag) {
    // then construct the object for each part
    var parts = tag.split(":");
    return { value: parts[0], id: parts[1] };
  });
Sign up to request clarification or add additional context in comments.

3 Comments

Quick Note: IE8 doesn't have .map(). Could use jQuery's .map() instead.
@BillCriswell: … or better just shim it
Yeah. Only said that cause it's tagged with jquery.
1

You can do it with two splits pretty easily.

var s = 'Aldi:01|Andi:02|Martin:03', 
    parts = s.split('|'), 
    source = [];

for( var i = 0, l = parts.length; i < l; i++ ){
  var part = parts[i].split(':');
  source.push({ value: part[0], id: part[1] });
}

2 Comments

I don't know where is the mistake, but it's doesn't work Bill
I copy and pasted right into the console and sources returned what you wanted.
1
var output = 'Aldi:01|Andi:02|Martin:03';
var data = output.split('|');
var source = new Array();

for(var key in data){
    var temp = data[key].split(':');
    source[key] = {'value':temp[0],'id':temp[1]};
}

By using,

for(var key in source){
    alert(source[key]['value']);
    alert(source[key]['id']);
}

1 Comment

Yeah. Actually I use that array for jQuery.autocomplete
0

First split the string by the bar character .split('|') to get an array of three items, then loop through each of those items and split them again for the value/id pairs and put it into your final array.

var responseEcho; // assume this is your response from the server.
var myArray = responseEcho.split('|');
var finalArray = [];
myArray.forEach(function(data) {
  var dataArray = data.split(':');
  finalArray.push({'value': dataArray[0], 'id': dataArray[1]});
});

You may want to include a bit more safety checking just in case, but that is the general idea.

5 Comments

Are you mixing PHP with JavaScript syntax here?
Everything is good besides the finalArray[]. It would need to be finalArray.push({value: dataArray[0], id: dataArray[1]}); Also the array setup. It would need finalArray = []
Great idea. But why myArray.forEach(function(data) doesn't work? I had tried
It's a newer standard. THe solution I added is pretty safe a ways back browser support wise.
@AldiUnanto sorry, I wrote it too fast and didn't check my work very well, I forgot to add the closing ); to the end of the forEach function call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.