Question

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"
      },
    ]
Was it helpful?

Solution

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

OTHER TIPS

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

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top