Banani720
Banani720

Reputation: 159

Get first array in object with length X

I am importing an excel file into a web app using xlsx library and each row in the excel sheet is stored into an object containing arrays with length equal to the number of cells in each row as shown below...

onFileChange(event: any) {
    const inputFile: DataTransfer = <DataTransfer>(event.target); 
    const fileReader: FileReader = new FileReader();
    fileReader.onload = (event: any) => {
      const binaryString: string = event.target.result;
      const workBook: XLSX.WorkBook = XLSX.read(binaryString, { type: 'binary', sheetStubs: true}); 
      /* sheetstubs true supposedly shows empty cells but isn't */
      console.log(typeof binaryString)

      const workSheetName: string = workBook.SheetNames[0];
      console.log(workSheetName)
      const workSheet: XLSX.WorkSheet = workBook.Sheets[workSheetName];

      this.data = <Array>(XLSX.utils.sheet_to_json(workSheet, { header: 1, blankrows: true }));


    };
    fileReader.readAsBinaryString(inputFile.files[0]);

  }

trying this below

I am trying to make another function getFirstMeaningfulRow which would spit out the first row with a length >= 5 let's say, would a for-loop be the appropriate method by which to accomplish this?

using the `find` function
```typescript
getFirstMeaningfulRow() {
    console.log(this.data.find(meaningfulRow => this.data.length >= 5

spits out the first array that contains a string with more than five characters in it. Ex.

first row imported contains one element, a date MM/DD/YYY so console.log shows

["10 SEPTEMBER, 2019"]
 0: "10 SEPTEMBER, 2019"

I am trying to find the first array in the object with more than 5 elements in that array so that console.log would show:

['item0', 'item1', 'item2', 'item3', 'item4',...]
  0: 'item0'
  1: 'item1'
  2: 'item2'
  3: 'item3'
  4: 'item4'
     ...

Upvotes: 0

Views: 326

Answers (1)

Damian Green
Damian Green

Reputation: 7535

I think you intended to use meaningfulRow rather than this.data.

You're comparing the length of the array not the length of the element.

  getFirstMeaningfulRow() {
       console.log(this.data.find(x=> x.length >= 5))
   }

Upvotes: 2

Related Questions