简体   繁体   English

如何在 Typescript 中推送数组中的元素

[英]How to push elements in an Array of Array in Typescript

I'm new to typescript.我是 typescript 的新手。 I've created a Monthpicker widget in Angular.我在 Angular 中创建了一个Monthpicker小部件。 Originally, It is very complicated and have lots of functionalities.最初,它非常复杂并且有很多功能。 So, I'll try to keep it as simple as possible.所以,我会尽量保持简单。 Here is my code.这是我的代码。

monthpicker.component.ts monthpicker.component.ts

import { Component } from '@angular/core';

@Component({
  ...
})
export class AppComponent  {

  monthsViewSlices: Array<Array<number>>;

  ngOnInit(): void {
    this.monthsViewSlices = [[0, 24], [6, 30], [18, 42], [30, 54], [42, 66], [54, 78]];
    console.log(this.monthsViewSlices);
  }
}

And these monthViewSlices are actually slices like this:而这些monthViewSlices实际上是这样的切片:

在此处输入图像描述

Month indexes are starting from 0. As you can see I've hard-coded slices as follows:月份索引从 0 开始。如您所见,我对切片进行了硬编码,如下所示:

this.monthsViewSlices = [[0, 24], [6, 30], [18, 42], [30, 54], [42, 66], [54, 78]];

Which is not a very good thing to do.这不是一件好事。 I want to make this configurable using for loops.我想使用for循环使其可配置。 I tried this code:我试过这段代码:

  ngOnInit(): void {
    const lowerBound=2012;
    const upperBound=2015;
    let x;
    let y;
    let z;
    let iterations=upperBound-lowerBound;
    for(x=lowerBound; x<=upperBound; x++) {
      for(y=0; y<=24; y++) {
        //DO NOTHING JUST ITERATE
      }
      this.monthsViewSlices.push([0,y]);
    }
    console.log(this.monthsViewSlices);
  }

But I don't know how to deal with Array<Array<number>> .但我不知道如何处理Array<Array<number>> I also tried taking help from these:我还尝试从这些方面寻求帮助:

  1. “push()” to an array in Typescript “push()” 到 Typescript 中的数组
  2. How to Push object in an array of array 如何在数组数组中推送 object
  3. typescript push elements into an array in roundrobin fashion typescript 以循环方式将元素推入数组
  4. Array of Arrays in TypeScript TypeScript 中的 Arrays 数组

Simple push() method will not work here.简单的push()方法在这里不起作用。 That I've tried.我试过了。 Or mayne not in the correct way.或者可能没有以正确的方式。 So, I've to a lot of gymnastics here.所以,我在这里做了很多体操。

But I'm not able to make this dynamic instead of hard coding.但我无法使这个动态而不是硬编码。

Following things should be taken care of:应注意以下事项:

  1. Number of slices are equal to number of iterations which is upperBound-lowerBound切片数等于iterations ,即upperBound-lowerBound
  2. Difference between first and second slice is 6 while for rest it is 12:第一个和第二个切片之间的差异是 6,而对于 rest,它是 12: 在此处输入图像描述

I've created a stackblitz also.我也创建了一个堆栈闪电战 Please someone help me achieve this.请有人帮我实现这一目标。 Let me know if more details are required to this question.让我知道这个问题是否需要更多细节。 And also tell me if it is even doable or I am wasting my and everyone's time.并告诉我这是否可行,或者我在浪费我和每个人的时间。

If I've read your example correctly, you want 3 "slices" (the difference between 2012 and 2015 in this case), resulting in如果我正确阅读了您的示例,则您需要 3 个“切片”(在这种情况下是 2012 年和 2015 年之间的差异),从而导致

[0, 24]
[6, 30]
[18, 42]

I've forked your stackblitz here我在这里分叉了你的堆栈闪电战

You weren't wrong with how you were manipulating your arrays (I personally prefer to type arrays as [] rather than Array<>, but that's just a personal preference), the loop logic just needed a little help.您操作数组的方式并没有错(我个人更喜欢将数组键入为 [] 而不是 Array<>,但这只是个人喜好),循环逻辑只需要一点帮助。

Edit: Code from the stackblitz fork:编辑:来自stackblitz fork的代码:

  years: number[] = [];
  monthsViewSlices: number[][] = [];

  ngOnInit(): void {
    const lowerBound=2012;
    const upperBound=2015;

    let x = 0;
    let y = 24;
    let iterations=upperBound-lowerBound;
    for(let i=0; i<iterations; i++) {      
      this.monthsViewSlices.push([x, y]);
      const increment = i === 0 ? 6 : 12;
      x+=increment;
      y+=increment;

    }
    this.monthsViewSlices.forEach(slice => console.log(slice));
  }

First, create an empty array like this let month: string[] = [];首先,像这样创建一个空数组let month: string[] = []; , you can use const or let, if you are declaring inside the function use let, otherwise use const or var, it's all up to you. ,你可以使用 const 或 let,如果你在 function 内部声明使用 let,否则使用 const 或 var,这完全取决于你。

you have an array this.monthsViewSlices = [[0, 24], [6, 30], [18, 42], [30, 54], [42, 66], [54, 78]];你有一个数组this.monthsViewSlices = [[0, 24], [6, 30], [18, 42], [30, 54], [42, 66], [54, 78]]; and in case you want the specific element to push in the new array you can use something like this.如果您希望特定元素推入新数组,您可以使用类似的东西。

    for(let month in monthsViewSlices){ 
       if monthsViewSlices[month].includes(value/condition) === true){
          month.push(monthsViewSlices[month])
          } 
        }

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

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