2

I am an array in following format. I am reading following data from a file data.json like this

$.getJSON('data/data.json', function (ids) {
            for (var i = 0; i < ids.length; i++) {
                var id = ids[i];
                teams = id;
                $('<li>' + id + '</li>').appendTo(span);

            }
        });

here is data

[
"109 200",
"109 201",
"102 202",
"103 202"
]

So what I want is to copy this array to another lets say c but in following form

[
"109",
"109",
"102",
"103",
"200",
"201",
"202",
"202"
]

How can I do this using javascript or jquery?

3
  • 2
    Ahmed's answer is the least amount of code and it is working. Commented Aug 15, 2012 at 11:20
  • @FrançoisWahl: No, the result is not in the order that OP requires. Commented Aug 15, 2012 at 11:57
  • @João: I see now. You are correct. Commented Aug 15, 2012 at 12:00

3 Answers 3

4

Iterate over the result set, split each element by whitespace, and add the resulting two numbers to two different arrays, each representing one halve, and finally, concatenate the left part with the right part:

var left = [], 
    right = [];
for (var i = 0; i < ids.length; i++) {
  var pair = ids[i].split(" ");
  left.push(pair[0]);
  right.push(pair[1]);
}
var result = left.concat(right);
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't look like that will get the ordering OP wanted. With the edit, it does. ;)
No problem. I'm just too bad at ECMAScript to be able to supply the correct solution so I'm glad you could.
3

simply use

ids.join(' ').split(' ');

Edit This will respect the order.

$.map(ids,function(i){ return i.split(' ')[0]; }).concat($.map(ids,function(i){ return i.split(' ')[1]; }))

2 Comments

+1 Very very nice and short solution. Seems to work just right keeping the order and splitting them up: jsfiddle.net/6rgd3
But it doesn't keep the order that OP requires.
1

Although João has the correct method to do this, I noticed the order in which you wanted these elements adding to the array. To get what you state you're after, try this:

$.getJSON('data/data.json', function (ids) {
    var leftArray = new Array();
    var rightArray = new Array();

    for (var i = 0; i < ids.length; i++) {
        var splitted = ids[i].split(" ");

        if (splitted[0] !== null) {
            leftArray.push(splitted[0]);
        }

        if (splitted[1] !== null) {
            rightArray.push(splitted[1]);
        }
    }

    // now join the two arrays together to get your order
    var joinedArray = leftArray.conact(rightArray);

    // now build your elements with the data
    for (var i = 0; i < joinedArray.length; i++) {
        $('<li>' + joinedArray[i] + '</li>').appendTo(span);
    }
});

Comments

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.