最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - copying one array to another array - Stack Overflow

programmeradmin7浏览0评论

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?

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?

Share Improve this question asked Aug 15, 2012 at 11:04 Om3gaOm3ga 33k45 gold badges149 silver badges230 bronze badges 3
  • 2 Ahmed's answer is the least amount of code and it is working. – Nope Commented Aug 15, 2012 at 11:20
  • @FrançoisWahl: No, the result is not in the order that OP requires. – João Silva Commented Aug 15, 2012 at 11:57
  • @João: I see now. You are correct. – Nope Commented Aug 15, 2012 at 12:00
Add a ment  | 

3 Answers 3

Reset to default 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);

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]; }))

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);
    }
});
发布评论

评论列表(0)

  1. 暂无评论