这是我的函数代码,我想在“ a”变量中返回2d数组;
var a = [[]];
var b = [[]];
var j = 0;
var i = 0;
let is = 2;
let desiredFields = ['display_name', 'phone'];
function myFunction() {
console.log('Loading contacts...');
let timer = new Date().getTime();
Contacts.getContactsWorker(desiredFields).then((result) => {
console.log(`Loading contacts completed in ${(new Date().getTime() - timer)} ms.`);
console.log(`Found ${result.length} contacts.`);
deger = result.length;
a = new Array(result.length);
for (i = 0; i < result.length; i++) {
for (j = 0; j < 2; j++) {
if (j == 0) a[i][j] = result[i].display_name;
if (j == 1) a[i][j] = result[i].phone[0].number;
}
}
});
return a;
}
这是调用该函数的代码;
getItems(): Array <IDataItem> {
b = myFunction();
while (is <= 5) {
this.items.push({
id: is,
name: b[0][0],
description: b[0][1]
});
is = is + 1;
}
return this.items;
}
我的代码不正确吗?如何将2d数组发送到函数?