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.

1
  • use push instead of merge Commented May 16, 2013 at 19:50

3 Answers 3

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

http://jsfiddle.net/FC5tL/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, That worked. nice and concise. but SO wants me to wait for 5 mins before i mark this as an answer.
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

Comments

0

on each iteration:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.