Arrays TS:自定义对象数组的定义
这是我的数组定义(可能有问题。我想说这个数组将是一个自定义对象数组): 那么我想填一下:Arrays TS:自定义对象数组的定义,arrays,typescript,Arrays,Typescript,这是我的数组定义(可能有问题。我想说这个数组将是一个自定义对象数组): 那么我想填一下: this.grid.gridView.data.forEach(element => { records.push(element.LicencePlate, element.Description, element.DateOfStartUse.toLocaleDateString('en-GB'), element.Base, element.Active);
this.grid.gridView.data.forEach(element => {
records.push(element.LicencePlate, element.Description, element.DateOfStartUse.toLocaleDateString('en-GB'),
element.Base, element.Active);
});
我想得到这样的东西——对象数组
[{"Johnny", "Actor", "05/03/2000", "Holywood", true},
{"Kirk", "Musician", "01/06/1999", "California", true},
{"Elvis", "Singer", "15/09/1975", "Mississippi", false}]
但我只得到了一个长的单值数组:
["Johnny", "Actor", "05/03/2000", "Holywood", true,
"Kirk", "Musician", "01/06/1999", "California", true,
"Elvis", "Singer", "15/09/1975", "Mississippi", false]
我哪里出错了?每次迭代都需要将新对象推入数组 比如:
this.grid.gridView.data.forEach(element => {
// create object for this row
const o = {
LicencePlate: element.LicencePlate,
Description: element.Description,
/// other properties and values
}
// push that object to array
records.push(o);
});
-编辑Oops,这是js,您需要ts。。漠视我把这个留在这里 除了charlietfl的回答之外
arr.push(element1[,…[,elementN]])
您希望在末尾将单个元素推入数组索引
但是您正在将元素的每个字段按新索引推入数组中
charlietfl的回答实质上是创建每个元素的浅副本,
您也可以直接推动该元素:
let records = []; //or new Array();
//typescript will let you do new Array<Record>(), but not in javascript
this.grid.gridView.data.forEach(element => {
records.push(element);
});
let records=[]//或新数组();
//typescript将允许您执行新数组(),但不能使用javascript
this.grid.gridView.data.forEach(元素=>{
记录。推送(元素);
});
我对记录的执行情况:
//An implementation of your record object (you apparently already have these, so you don't need it)
class Record {
constructor (licensePlate) {
this.licensePlate = licensePlate;
this.description = description;
this.setToUse = setToUse;
this.depot = depot;
this.active = active;
}
}
let records = new Array(); //In typescript we can have Array<Record> for typed array, but not in js :(
//I'm creating a test record here because I don't have your gridView code
let rec = new Record("BLAHBLAH", "A record example instance", "some kind of set to use", "a depot", false);
records.push(rec); //Push the record instance|object into the array
//this.grid.gridView.data.forEach(element => {
// records.push(element.LicencePlate, element.Description, element.DateOfStartUse.toLocaleDateString('en-GB'),
// element.Base, element.Active);
//});
//记录对象的实现(显然您已经有了这些,所以不需要它)
课堂记录{
建造商(牌照){
this.licensePlate=licensePlate;
this.description=描述;
this.settuse=settuse;
this.depot=仓库;
这个.active=active;
}
}
让记录=新数组()//在typescript中,我们可以将数组用于类型化数组,但在js中不能:(
//我在这里创建一个测试记录,因为我没有您的gridView代码
让rec=新记录(“BLAHBLAH”,“记录示例实例”,“要使用的某种集合”,“仓库”,false);
records.push(rec);//将记录实例|对象推入数组
//this.grid.gridView.data.forEach(元素=>{
//records.push(element.licenseplate、element.Description、element.DateOfStartUse.toLocaleDateString('en-GB'),
//element.Base,element.Active);
//});
//An implementation of your record object (you apparently already have these, so you don't need it)
class Record {
constructor (licensePlate) {
this.licensePlate = licensePlate;
this.description = description;
this.setToUse = setToUse;
this.depot = depot;
this.active = active;
}
}
let records = new Array(); //In typescript we can have Array<Record> for typed array, but not in js :(
//I'm creating a test record here because I don't have your gridView code
let rec = new Record("BLAHBLAH", "A record example instance", "some kind of set to use", "a depot", false);
records.push(rec); //Push the record instance|object into the array
//this.grid.gridView.data.forEach(element => {
// records.push(element.LicencePlate, element.Description, element.DateOfStartUse.toLocaleDateString('en-GB'),
// element.Base, element.Active);
//});