空数组不为空

时间:2017-08-07 05:54:50

标签: arrays angular typescript ionic-framework

我的功能如下:

public returnData(): { points: Array<{ x: number, y: number }>, pointsCount: Array<number> } {
    return {
        points: [{x: 0, y: 1},{x: 1, y: 2 }],
        pointsCount: [1, 2, 3, 4]
    }
 }

如果我会写下这样的话:

let mydata = this.returnData();

对象包含两个数组:{points:Array(2),pointsCount:Array(4)}

但是,如果我要改变它:

public returnData(): { points: Array<{ x: number, y: number }>, pointsCount: Array<number> } {

     let localdata: Array<{x:number, y:number}> = [];
     let local_points_count: Array<number> = [];

     localdata.push([{x:0,y:1},{x:1,y:2}]);
     local_points_count.push([1,2,3,4]);

    return {
        points: localdata,
        pointsCount: local_points_count
    }
 }
let mydata = this.returnData();

将返回{points:Array(0),pointsCount:Array(0)}。如果我将在Chrome控制台中查看数据,则表明它不是空的。

请帮帮我!

2 个答案:

答案 0 :(得分:3)

您正在将数组推入数组。试试这个

localdata.push({x:0,y:1},{x:1,y:2});
local_points_count.push(1,2,3,4);

答案 1 :(得分:0)

首先,你有[{x:0,y:1],{x:1,y:2}] ( { ... } not {])。 ÿ

你会写localdata.push([{x:0,y:1},{x:1,y:2}]);

let local_points_count:Array = [];你有数字或不数组数组。