在for循环javascript中创建一个数组

时间:2022-11-28 01:27:14

I am trying to create a dataset pattern like this from the rest service in my angular application.

我试图从我的角度应用程序中的其余服务创建这样的数据集模式。

Example:

例:

$scope.chartSeries = [
  {"name": "Google", "data": [100, 200, 400, 700, 300]},
  {"name": "Yahoo", "data": [300, 100, null, 500, 200]},
  {"name": "Facebook", "data": [500, 200, 200, 300, 500] },
  {"name": "Microsoft", "data": [100, 100, 200, 300, 200] }
];

Right now this is my code to build a similar pattern ,

现在这是构建类似模式的代码,

 if (datai){ 
    $rootScope.DashboardData =[];
    $rootScope.DashboardData = datai;
    var _fieldData = [];
    widget.chartConfig.series  = [];
    widget.chartConfig.series  = 
    $rootScope.DashboardData.map(function(elm) {
        return { 
            name: elm[widget.seriesname], 
            data:_fieldData.push(elm[widget.dataname])
        };
    });
 }

Problem is data field is not creating an array. This is the final output which the above code generates.

问题是数据字段没有创建数组。这是上面代码生成的最终输出。

 [{"name": "Google", "data": 1}];

4 个解决方案

#1


2  

You are assigning _fieldData.push(elm[widget.dataname]) to data and .push() method returns the new array length, so you need to push the element first and then assign the array itself.

您将_fieldData.push(elm [widget.dataname])分配给数据,而.push()方法返回新的数组长度,因此您需要先推送元素然后分配数组本身。

_fieldData.push(elm[widget.dataname])
 return { name: elm[widget.seriesname], data:_fieldData};
      });

#2


3  

push() returns the new array's length

push()返回新数组的长度

$rootScope.DashboardData.map(function(elm) 
{
    var _fieldData = [];
    _fieldData.push(elm[widget.dataname]);
    return { name: elm[widget.seriesname], data: _fieldData };
});

or you can use concat()

或者你可以使用concat()

return { name: elm[widget.seriesname], data: _fieldData.concat(elm[widget.dataname])};                                          

#3


2  

try this , You nee a loop for dataname

试试这个,你需要一个数据名称的循环

$rootScope.DashboardData.map(function(elm) 
{
   var _fieldData = [];
   for(var i=0;i<elm[widget.dataname.length;i++)
    {
      _fieldData.push(elm[widget.dataname][i]);
    }    
    return { name: elm[widget.seriesname], data: _fieldData
});

#4


0  

Array .push() always returns an new length of an array

Array .push()始终返回数组的新长度

You should push that variable into _fieldData.push(elm[widget.dataname]); then assign that variable late in data.

你应该将该变量推送到_fieldData.push(elm [widget.dataname]);然后在数据后期分配该变量。

$rootScope.DashboardData.map(function(elm) 
{
    var _fieldData = [].push(elm[widget.dataname]);
    return { name: elm[widget.seriesname], data: _fieldData }
});

#1


2  

You are assigning _fieldData.push(elm[widget.dataname]) to data and .push() method returns the new array length, so you need to push the element first and then assign the array itself.

您将_fieldData.push(elm [widget.dataname])分配给数据,而.push()方法返回新的数组长度,因此您需要先推送元素然后分配数组本身。

_fieldData.push(elm[widget.dataname])
 return { name: elm[widget.seriesname], data:_fieldData};
      });

#2


3  

push() returns the new array's length

push()返回新数组的长度

$rootScope.DashboardData.map(function(elm) 
{
    var _fieldData = [];
    _fieldData.push(elm[widget.dataname]);
    return { name: elm[widget.seriesname], data: _fieldData };
});

or you can use concat()

或者你可以使用concat()

return { name: elm[widget.seriesname], data: _fieldData.concat(elm[widget.dataname])};                                          

#3


2  

try this , You nee a loop for dataname

试试这个,你需要一个数据名称的循环

$rootScope.DashboardData.map(function(elm) 
{
   var _fieldData = [];
   for(var i=0;i<elm[widget.dataname.length;i++)
    {
      _fieldData.push(elm[widget.dataname][i]);
    }    
    return { name: elm[widget.seriesname], data: _fieldData
});

#4


0  

Array .push() always returns an new length of an array

Array .push()始终返回数组的新长度

You should push that variable into _fieldData.push(elm[widget.dataname]); then assign that variable late in data.

你应该将该变量推送到_fieldData.push(elm [widget.dataname]);然后在数据后期分配该变量。

$rootScope.DashboardData.map(function(elm) 
{
    var _fieldData = [].push(elm[widget.dataname]);
    return { name: elm[widget.seriesname], data: _fieldData }
});