繁体   English   中英

如何将元素按所需格式放入数组

[英]How can I push the elements into array in a desired format

我想将元素以所需的格式放入数组中,我的代码是:

 var arr = [];
    for(var i = 0; i < $scope.reportRangeList.length; i++)
    {
     arr.push($scope.reportRangeList[i].businessName  , $scope.reportRangeList[i].total1);
     alert(JSON.stringify(arr));
    }

其中$scope.reportRangeList是一个JSON动态对象

对于上面的代码,我得到如下输出:

[
    "abc",1820,
    "pqrs",2349.67

 ]     

现在我想显示输出:

[
    "abc",1820
],

 [    "pqrs",2349.67

 ]

请帮助我在Java脚本或angular或j查询中以所需格式显示输出。

你可以像这样推一个数组

arr.push([$scope.reportRangeList[i].businessName  , $scope.reportRangeList[i].total1]);

对于这种格式,每次像下面这样推一个数组

arr.push([$scope.reportRangeList[i].businessName  , $scope.reportRangeList[i].total1]);

您想要的格式

var first = $scope.reportRangeList[i].businessName,
    second = $scope.reportRangeList[i].total1,
    text;

arr.push([first, second]);

text = JSON.stringify(arr); 
text = text.substring(1, text.length - 1);
alert(text);

JSFiddle

暂无
暂无

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

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