简体   繁体   English

我需要将 arrays 数组转换为对象的 arrays

[英]I need to convert an array of arrays into an arrays of objects

I am trying to convert an array of arrays into an array of objects, I am working with Angular and TypeScript, this is the code:我正在尝试将 arrays 数组转换为对象数组,我正在使用 Angular 和 TypeScript,这是代码:

look(data: [][]){
    
    const found = data.flatMap( element => element ).find(element => element == this.codigo );
    console.log(found);

    const dataobj = data.map(function(row){
      return {
        a: row[0],
        b: row[1],
        c: row[2],
        d: row[3],
        e: row[4],
        f: row[5],
        g: row[6],
      }
    });
    console.log(dataobj);
    console.log(this.datos);
    

  }

And I am getting this error: src/app/hojaexcel/hojaexcel.component.ts:61:16 - error TS2493: Tuple type '[]' of length '0' has no element at index '0'.我收到此错误:src/app/hojaexcel/hojaexcel.component.ts:61:16 - 错误 TS2493:长度为“0”的元组类型“[]”在索引“0”处没有元素。

61 a: row[0], ~ 61 a: 行 [0], ~

The type [] is a tuple type and not an array type. []类型是元组类型,而不是数组类型。 You can have [number] , [number, string] etc... these are all tuple types.你可以有[number][number, string]等等......这些都是元组类型。 [] would be a tuple with no elements. []将是一个没有元素的元组。 [][] is a tuple of a tuple with no elements, literally [[]] . [][]是没有元素的元组的元组,字面意思是[[]]

You need to declare a type for it to be an array of an array.您需要声明一个类型以使其成为数组的数组。 I'm not sure what data is supposed to be, but let's assume it is a number:我不确定data应该是什么,但我们假设它是一个数字:

look(data: number[][]) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM