1

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.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
user1
  • 1,063
  • 1
  • 8
  • 28

3 Answers3

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

http://jsfiddle.net/FC5tL/

Ram
  • 143,282
  • 16
  • 168
  • 197
  • Thanks, That worked. nice and concise. but SO wants me to wait for 5 mins before i mark this as an answer. – user1 May 16 '13 at 19:58
0

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

Community
  • 1
  • 1
mCube
  • 334
  • 3
  • 8
0

on each iteration:

arr2 = arr2.concat(tempdata.split(':'))
Guard
  • 6,816
  • 4
  • 38
  • 58