-1

This is my array definition (probably there is a problem. I wanted to say that this array will be an array of custom objects):

const records: {LicencePlate: string, Description: string, 
SetToUse: string, Depot: string, Active: boolean}[] = [];

Then I want to fill it:

  this.grid.gridView.data.forEach(element => {
      records.push(element.LicencePlate, element.Description, element.DateOfStartUse.toLocaleDateString('en-GB'),
      element.Base, element.Active);
    });

I wanted to get something like this - array of objects

[{"Johnny", "Actor", "05/03/2000", "Holywood", true}, 
 {"Kirk", "Musician", "01/06/1999", "California", true}, 
 {"Elvis", "Singer", "15/09/1975", "Mississippi", false}]

But I got only one long array of single values:

["Johnny", "Actor", "05/03/2000", "Holywood", true, 
"Kirk", "Musician", "01/06/1999", "California", true, 
"Elvis", "Singer", "15/09/1975", "Mississippi", false]

Where have I made a mistake?

0

2 Answers 2

0

You need to push a new object into the array every iteration.

Something like:

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);
});
Sign up to request clarification or add additional context in comments.

Comments

0

-edit Oops, this is js and you want ts.. Disregard. I'll leave this here.

In addition to charlietfl's answer, the documentation on <array>.push:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

arr.push(element1[, ...[, elementN]])

You're wanting to push a single element into an array index at the end,

but you are pushing each field of your element into the array at a new index instead.

charlietfl's answer essentially creates a shallow duplicate of each element, you could alternatively just push the element directly:

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);
});

My implementation of records:

//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);
//});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.