Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Help a newbie to solve the following problem. I have an object:

var arr = [
    [{a:1},{a:2}],
    [{b:1},{b:2}]
    ];

How to convert it to this:

var arr1 = [
    [{a:1},{b:1}],
    [{a:2},{b:2}],
    ];

Many thanks to all for your answers! I'm trying to split the json response from the server to the individual objects. I have here a json: var src = {

    img: [
            {
                alt: "",
                src: "http://1.jpg",
                width: "125"
            },
            {
                alt: "",
                src: "http://2.jpg",
                width: "125"
            }

        ],
    a: [
            {
                href: "http://1.html",
                content: "title1"
            },
            {
                href: "http://2.html",
                content: "title2"
            }
        ],
    dd: [
            {
                p: "content1"
        },
        {
                p: "content2"
        }

        ]
}

I want to convert the json individual objects:

    var src1 = [
       {
          alt: "",
          src: "http://1.jpg",
          width: "125",
          href: "http://1.html",
          content: "title1",
          p: "content1"
      },
{
          alt: "",
          src: "http://2.jpg",
          width: "125",
          href: "http://2.html",
          content: "title2",
          p: "content2"
      },
    ]
share|improve this question
4  
Please clarify or give more details or more elaborated example of what you're looking to do. –  Khnle - Kevin Le Mar 4 at 4:12
    
@Khnle-KevinLe I think it is totally clear what he is trying to do. –  thefourtheye Mar 4 at 4:25
    
I have updated my answer with your updated JSON. Have a look and let me know whether it works or not. –  AbhinavRanjan Mar 4 at 8:20

3 Answers 3

up vote 0 down vote accepted

You need to do something like this. Rest you need to figure out based on your real data.

var arr1 = [];
var firstTime = 1;
for(var i = 0; i < arr.length; i++) {
    if(firstTime == 1){
        for(var j=0; j < arr[0].length; j++){

            arr1.push(new Array());
        }
        firstTime = 0;
    }
    for(var j=0; j < arr[0].length; j++){

        arr1[j].push(arr[i][j]);
    }
}

After the question is updated. You can use the following method to get what you want.

var src2 = new Array();
function convert(object){
    var firstTime = 1;
    for(var key in object){
        var property = object[key];
        if(firstTime == 1){
            for(var i = 0; i < property.length; i++){
                src2.push(new Object());
            }
            firstTime = 0;
        }
        for(var i = 0; i < property.length; i++){
            for(var key1 in property[i]){
                src2[i][key1] = property[i][key1];
            }
        }
    }
}

Usage: convert(src1);

share|improve this answer
    
Thank you very much! –  libtool Mar 4 at 9:21

Here is what I got.

Basically I break the complex array up into a single array. Do a comparison, then rebuild the array to the same dimensions of the initial array.

JS Fiddle: http://jsfiddle.net/49g69/

var arr = [
    [{a:1},{a:2}],
    [{b:1},{b:2}]
];

var newArr = []

//Step 1: Get all the data into one array
for(var i = 0; i<arr.length; i++) {
    for(var j = 0; j<arr[i].length; j++) {
        newArr.push(arr[i][j]);
    }
}

//Step 2: Sort that data
newArr.sort(function(a, b) {
    var aVal = a.a || a.b
      , bVal = b.a || b.b;
    return aVal - bVal; 
});

var finalArray = []
  , tempArray = [];

//Step 3: Get the Array size of the initial Array
var arrayCount = arr[0] ? arr[0].length : 0;

//Step 4: Loop through the sorted Array
for(var i = 0; i<newArr.length; i++) {

    //Step 5 Determine if it meets the size reqs, then push the subarray
    if((i+1) % arrayCount === 0 ) {
        tempArray.push(newArr[i]);
        finalArray.push(tempArray);
        tempArray = [];
    } else {
        tempArray.push(newArr[i]);
    }

}

console.log(finalArray);
share|improve this answer

It would be easier to help if you gave more detail about your task. Are you trying to group the internal pairs by the keys (ascending)? or values (equal)? any other/additional criteria?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.