0

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'
     ...
3
  • You can use array.find to get the first row with predicate row.length >=5. Commented Jan 7, 2020 at 18:25
  • 1
    @ford04 I tried this already but this spits out the first row with an array that has a string length of greater than 5 I am more concerned with finding the first array in the object of arrays with more than 5 elements see updated question, should've included that sorry Commented Jan 7, 2020 at 18:43
  • edited one more time for some formatting errors Commented Jan 7, 2020 at 18:52

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

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.