user1
user1

Reputation: 1065

Pop array element, split it and save it into different array

I want to pop array element, split it and save into different array. say I have two array

arr1 = ["a:b", "c:d"]
arr2 = []

I want to have arr2 as

arr2 = ["a", "b", "c", "d"]

I tried

var arr1 = ["a:b", "c:d"]

var arr2 = [];

        var tempdata;

        for (var i = 0; i < arr1.length; i++) {
            tempdata = arr1.pop();

            arr2.merge(tempdata.split(':'));
        }

but firebug gives me an error saying merge is not a function.

I also tried

var arr1 = ["a:b", "c:d"]

var arr2 = [];

        var tempdata;


        for (var i = 0; i < arr1.length; i++) {
            tempdata = arr1.pop();
            var temparray = [];
            temparray = tempdata.split(':'); 
            arr2.merge(temparray);
        }

still no luck.

Thanks for the help. PS:I don't mind using Jquery.

Upvotes: 1

Views: 454

Answers (3)

Guard
Guard

Reputation: 6955

on each iteration:

arr2 = arr2.concat(tempdata.split(':'))

Upvotes: 0

Ram
Ram

Reputation: 144689

var arr2 = $.map(arr1, function(elem) {
    return elem.split(':');
});

http://jsfiddle.net/FC5tL/

Upvotes: 2

mCube
mCube

Reputation: 342

try converting the first array to string then convert it back to array just like this

var trainindIdArray = traingIds.split(',');
$.each(trainindIdArray, function(index, value) { 
    alert(index + ': ' + value);   // alerts 0:[1 ,  and  1:2]
});

from Javascript/Jquery Convert string to array question...

happy coding..:D

Upvotes: 0

Related Questions