简体   繁体   English

如何将数组对象转换为对象

[英]How to convert Object of Arrays to Objects

I have some data coming from the MVC controller in the below format: 我有一些来自MVC控制器的数据,格式如下:

{id: Array[3], city: Array[3]}

I wanted to convert that data into 我想将这些数据转换为

[Object, Object, Object]

which will have the structure Object0{id, city} , Object1{id, city} , Object2{id, city} 它将具有结构Object0{id, city}Object1{id, city}Object2{id, city}

I tried the below method but didnt work out 我尝试了以下方法,但没有解决

angular.forEach(data, function(){

    vm.Cities = {
        id :data.citiesIDs,
        city : data.citiesStr
    }

});

Can anyone please give me a hint as in where i am going wrong or what is the best way to achieve this. 任何人都可以给我一个提示,告诉我我要去哪里了或者实现这一目标的最佳方法是什么。 Thanks in advance. 提前致谢。

You don't really need Angular for this, plain Javascript works just as well. 您实际上并不需要Angular,普通的Javascript也可以。

function transform(object) {
  var result = [];
  for (var i = 0; i < object.id.length; i++) {
    result.push({
      id: object.id[i],
      city: object.city[i]
    });
  }
  return result;
}

Then you can call your helper with your data: 然后,您可以使用数据呼叫助手:

var list = transform(data); // <-- list of (id,city) objects

Keep in mind the function assumes both of your id and city arrays are of the same length (which really wouldn't make sense if they weren't), BUT for the case they're not of the same length, you would want to make a minor change in your for loop: 请记住,该函数假定您的idcity数组都具有相同的长度(如果它们的长度不同,这实际上是没有意义的),但是对于它们的长度不相同的情况,则需要在您的for循环中做一个小改动:

var maxLen = Math.max(object.id.length, object.city.length);
for (var i = 0; i < maxLen; i++)

This is a simple JS operation and here is the demo 这是一个简单的JS操作, 这是演示

// Assuming obj is defined and both obj.id and obj.city are arrays

var obj = {
    id: [25, 82, 188, 141],
    city: ['Tokyo', 'Munich', 'Los Angeles', 'Sao Paolo'],
};

var max = Math.max(obj.id.length, obj.city.length);

var results = [];

for(var i = 0; i < max; i++) {
    var converted = {
        id: obj.id[i] ? obj.id[i] : null,
        city: obj.city[i] ? obj.city[i] : null
    };
    results.push(converted);
}

console.log('Coverted array', results);

Very simple example. 非常简单的例子。 Iterate over one of the arrays and grab from the other by index. 遍历一个数组,并通过索引从另一个数组中获取。

var cities = []
angular.forEach(data.id, function(id, index) {
    var city = {id: id, city: data.city[index]};
    cities.push(city);
});

Iterator can help you. 迭代器可以为您提供帮助。 eg: 例如:

var data = {id: [1,2], city: ['Curitiba','São Paulo']};
    var array = [];
    for(var prop in data){
        var length = data[prop].length;
        for(var z = 0; z < length; z++){
            if(!array.hasOwnProperty(z)){
                array[z] = {};
            }
            array[z][prop] = data[prop][z];
        }
    }
    console.log(array);// [Object{city:'Curitiba',id:1},Object{city:'São Paulo',id:2}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM