Angular 7和将两个数组推入一个数组的数组给出未定义的错误

时间:2019-03-22 08:10:00

标签: arrays angular typescript angular7

我需要将从excel文件中读取的2个数组合并为2维数组,以便将其作为物料表的dataSource放置。

我有两个使用XLSX库从excel中读取的数组:

    reader.onload = (e) => {
      const res = reader.result as string; // This variable contains your file as text
      const lines = res.split('\n'); // Splits you file into lines
      let ids=[];
      let name = [];
      let array:any[][];
      lines.forEach((line, index) => {
        //console.log(line);
        ids.push((line.split(',')[0]));
        name.push(line.split(',')[1]);
        array.push([ids, name])
      });
      console.log(array);
    }

但是我在console.log(array)上总是遇到错误:

  

错误TypeError:无法读取未定义的属性“ push”

编辑

我将代码更改为:

      let name = [];

      lines.forEach((line, index) => {
        //console.log(line);
        ids.push((line.split(',')[0]));
        name.push(line.split(',')[1]);
        array.push(ids, name)
      });
      console.log(array);

结果是这样的:

enter image description here

但这不是我所需要的,因为它不能用作物料表上的数据源。

1 个答案:

答案 0 :(得分:1)

如果我正确解决了您的问题...这是实现您的问题的简单方法 需求。

@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
public class DemoApplicationTests {

    volatile DemoApplication app;

    volatile ConfigurableApplicationContext context;

    private VideoService videoService;

    @Test
    public void contextLoads() throws RunnerException {
        Options opt = new OptionsBuilder()
            .include(DemoApplicationTests.class.getSimpleName())
            .forks(1)
            .build();
        new Runner(opt).run();
    }

    @Setup
    public void setup() {
        this.context = new SpringApplication(DemoApplication.class).run();
        Object o = this.context.getBean(VideoService.class);
        videoService = (VideoService)o;
    }

    @TearDown
    public void tearDown(){
        this.context.close();
    }

    @Benchmark
    public String benchmark(){
        return videoService.find("z");
    }