如何从数组列表中创建数组对象。 (使用angularjs)

时间:2022-06-10 12:15:08

I have a list of arrays with me. Here it is

我有一个阵列列表。这里是

array1= [ab,bc];
array2= [12,23];
array3= [vw,wx];

Now i need to make an object of array like the one mentioned below from the above list

现在我需要从上面的列表中创建一个类似于下面提到的数组的对象

[ab,12, vw] as 1st array
[bc,23,wx] as 2nd array

And these two arrays should now be in a single object. how can i achieve this ?

现在这两个数组应该在一个对象中。我怎么能实现这个目标?

3 个解决方案

#1


1  

Check this out. This is more dynamic implementation with no hard-coding of assumptions.

看一下这个。这是更动态的实现,没有硬编码的假设。

function prepareObjectOfArrays(myArrays){
    var myObj = [];
    var maxArrLength = calculateMaxLength(myArrays);
    for(var i=0; i<maxArrLength; i++){
        var tempArr = [];
        for(var j=0; j<myArrays.length; j++){
            tempArr.push(myArrays[j][i]);
        }
        myObj.push(tempArr);
    }
    console.log(myObj);
}

function calculateMaxLength(myArrays){
    var maxLength = 0;
    for(var j=0; j<myArrays.length; j++){
        var tempLength = myArrays[j].length;
        if(maxLength < tempLength){
            maxLength = tempLength;
        }
    }
    return maxLength;
}

Now invoke as below:

现在调用如下:

var array1= ['ab','bc'];
var array2= [12,23];
var array3= ['vw','wx'];
prepareObjectOfArrays([array1, array2, array3]);

As I said, this is more dynamic implementation with no hard-coding of assumptions, and will even work with more type of inputs. There could be more corner cases but this is almost fully dynamic. Check with below inputs:

正如我所说,这是更加动态的实现,没有硬编码的假设,甚至可以使用更多类型的输入。可能会有更多的角落情况,但这几乎是完全动态的。检查以下输入:

var array1= ['ab','bc','de', 'er'];
var array2= [12,23,34];
var array3= ['vw','wx','yz'];

var array1= ['ab','bc','de', 'er'];
var array2= [12,23,34,45,56,78];
var array3= ['vw','wx','yz'];

#2


2  

You can use zip function from lodash library.

您可以使用lodash库中的zip函数。

_.zip([ab, bc], [12, 23], [vw, wx]);
// -> [[ab, 12, vw], [bc, 23, wx]]

#3


2  

try something like this:

尝试这样的事情:

array1= ['ab','bc',33];
array2= [12,23,35];
array3= ['vw','wx',34,'hh'];
array4= ['vw1','wx1',36];
//object
var mixarrays = {
  minmax:'min',//decide col max or min
  decide_size:function(arrs){
    var size = this.minmax=='max'? 0 :999999;
    for(i in arrs){
      if(arrs[i].length > size && this.minmax=='max'){
        size = arrs[i].length;
      }else if(arrs[i].length < size && this.minmax=='min'){
        size = arrs[i].length;
      }
    }
    return size;
  },
  init:function(arrs){
    cols = this.decide_size(arrs);
    var mixedarray = [];
    for(var i=0;i<cols;i++){
      mixedarray[i] = [];
      for(j in arrs){
        mixedarray[i].push(arrs[j][i]);
      }
    }
    return mixedarray;
  }
};

//[array1,array2,array3,array4];//array of arrays
console.log(mixarrays.init([array1,array2,array3,array4]));

jsfiddle

的jsfiddle

#1


1  

Check this out. This is more dynamic implementation with no hard-coding of assumptions.

看一下这个。这是更动态的实现,没有硬编码的假设。

function prepareObjectOfArrays(myArrays){
    var myObj = [];
    var maxArrLength = calculateMaxLength(myArrays);
    for(var i=0; i<maxArrLength; i++){
        var tempArr = [];
        for(var j=0; j<myArrays.length; j++){
            tempArr.push(myArrays[j][i]);
        }
        myObj.push(tempArr);
    }
    console.log(myObj);
}

function calculateMaxLength(myArrays){
    var maxLength = 0;
    for(var j=0; j<myArrays.length; j++){
        var tempLength = myArrays[j].length;
        if(maxLength < tempLength){
            maxLength = tempLength;
        }
    }
    return maxLength;
}

Now invoke as below:

现在调用如下:

var array1= ['ab','bc'];
var array2= [12,23];
var array3= ['vw','wx'];
prepareObjectOfArrays([array1, array2, array3]);

As I said, this is more dynamic implementation with no hard-coding of assumptions, and will even work with more type of inputs. There could be more corner cases but this is almost fully dynamic. Check with below inputs:

正如我所说,这是更加动态的实现,没有硬编码的假设,甚至可以使用更多类型的输入。可能会有更多的角落情况,但这几乎是完全动态的。检查以下输入:

var array1= ['ab','bc','de', 'er'];
var array2= [12,23,34];
var array3= ['vw','wx','yz'];

var array1= ['ab','bc','de', 'er'];
var array2= [12,23,34,45,56,78];
var array3= ['vw','wx','yz'];

#2


2  

You can use zip function from lodash library.

您可以使用lodash库中的zip函数。

_.zip([ab, bc], [12, 23], [vw, wx]);
// -> [[ab, 12, vw], [bc, 23, wx]]

#3


2  

try something like this:

尝试这样的事情:

array1= ['ab','bc',33];
array2= [12,23,35];
array3= ['vw','wx',34,'hh'];
array4= ['vw1','wx1',36];
//object
var mixarrays = {
  minmax:'min',//decide col max or min
  decide_size:function(arrs){
    var size = this.minmax=='max'? 0 :999999;
    for(i in arrs){
      if(arrs[i].length > size && this.minmax=='max'){
        size = arrs[i].length;
      }else if(arrs[i].length < size && this.minmax=='min'){
        size = arrs[i].length;
      }
    }
    return size;
  },
  init:function(arrs){
    cols = this.decide_size(arrs);
    var mixedarray = [];
    for(var i=0;i<cols;i++){
      mixedarray[i] = [];
      for(j in arrs){
        mixedarray[i].push(arrs[j][i]);
      }
    }
    return mixedarray;
  }
};

//[array1,array2,array3,array4];//array of arrays
console.log(mixarrays.init([array1,array2,array3,array4]));

jsfiddle

的jsfiddle