使用切片函数切片JSON对象

时间:2013-08-06 03:46:07

标签: json angularjs

我想切片JSON数组,但得到以下错误:

  

对象#没有方法'slice'

以下是我的代码:

$scope.getPagedDataAsync = function (pageSize, page, searchText) {
    setTimeout(function () {
        var data;
        programService.query({
          id: $routeParams.id
        }, function (result) {
          data = {
            'program': result
          };
          data = JSON.stringify(data);
          data = JSON.parse(data);
          $scope.setPagingData(data,page,pageSize);

        });
    }, 100);

};

 $scope.setPagingData = function(data, page, pageSize){
    var pagedData = data.slice(0, 3);
    $scope.myData = pagedData;
    $scope.totalServerItems = data.length;
    if (!$scope.$$phase) {
        $scope.$apply();
    }
};

JSON数据:

{programId:1, 
 programName:project1, 
 programContent:content1, 
 programStartDate:2012-01-01, 
 templateId: '1'}

我想按如下方式对数组进行切片:programId,1,programName,project1,...

我很困惑,请帮忙。

1 个答案:

答案 0 :(得分:0)

尝试

var data = {
    programId:1, 
    programName:'project1', 
    programContent:'content1', 
    programStartDate:'2012-01-01', 
    templateId: '1'
}
var array = [];
for(var key in data){
    if(!data.hasOwnProperty(key)){
        continue;
    }
    array.push(key, data[key])
}
console.log(array, array.slice(0, 4))