最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to push array in array in angular? - Stack Overflow

programmeradmin3浏览0评论

Here I put all consoles and code below. I want to push array inside array, ex. in imagesArray has 3 arrays and now I push result array which has 2 arrays so first array is pushed properly in imagesArray but last array shows undefined and not pushed and gives error like imageArray undefined so how to push that? (I put consoles and codes below)

Before push object consoleimagesArray

result that i want to push result

after pushing console is like this

add-folderponent.ts

imagesArray : UploadedImages[] = [];

dialogRef.afterClosed().subscribe(result => {
  if(result != '' && result != undefined && result != null){
    for(var i = 1; i <= result.length ; i++){
      this.imagesArray.push(result[i]);
    }
  } 
});

add-folderponent.html

<div>
  <mat-card *ngFor="let images of imagesArray" style="height : 100px;width : 100px">
    <b>{{images.imageName}}</b> 
    <b>{{images.filesize}}</b>
  </mat-card>
</div>

Here I put all consoles and code below. I want to push array inside array, ex. in imagesArray has 3 arrays and now I push result array which has 2 arrays so first array is pushed properly in imagesArray but last array shows undefined and not pushed and gives error like imageArray undefined so how to push that? (I put consoles and codes below)

Before push object consoleimagesArray

result that i want to push result

after pushing console is like this

add-folder.ponent.ts

imagesArray : UploadedImages[] = [];

dialogRef.afterClosed().subscribe(result => {
  if(result != '' && result != undefined && result != null){
    for(var i = 1; i <= result.length ; i++){
      this.imagesArray.push(result[i]);
    }
  } 
});

add-folder.ponent.html

<div>
  <mat-card *ngFor="let images of imagesArray" style="height : 100px;width : 100px">
    <b>{{images.imageName}}</b> 
    <b>{{images.filesize}}</b>
  </mat-card>
</div>
Share Improve this question asked Nov 30, 2018 at 11:59 DharmeshDharmesh 6,00318 gold badges49 silver badges67 bronze badges 3
  • 2 change var i=1 to var i=0 in for loop and this i <= result.length to this i < result.length – Artyom Amiryan Commented Nov 30, 2018 at 12:02
  • arrays index position starts from 0! – Prashant Pimpale Commented Nov 30, 2018 at 12:03
  • 1 if (result != '' && result != undefined && result != null) is equal to if (result) – Eliseo Commented Nov 30, 2018 at 12:46
Add a ment  | 

5 Answers 5

Reset to default 4

This is the most recent updated way to push an array into an array in typescript. You can use spread operator to extend an array by easily

array1: [...array1, ...array2]

This will extend the array1 and add elements in the array2 into the array1.

You can find more about this in - https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

In this code

imagesArray : UploadedImages[] = [];

dialogRef.afterClosed().subscribe(result => {
    if(result != '' && result != undefined && result != null){
         for(var i = 1; i <= result.length ; i++){
             this.imagesArray.push(result[i]);
         }
    } 
});

You're starting the array from the position 1. Arrays starts at 0. So, you have 2 elements in your array, [element1, element2]. Your code first will get the position number 1 (element2), and insert on the other array.

And, the next iteration, i will be equals 2, which is equals to result.length, this.imagesArray.push(result[i]); will try to get the position 2, which don't exists, and push undefined into the imagesArray.

Change your code to:

for(var i = 0; i < result.length ; i++){
    this.imagesArray.push(result[i]);
}

The length of List will return 2 and the index of an array starts from 0 so the result.length - 1.

So you can do like this:

Change:

var i = 0; // Start from zero because arrays are starts from `0` so we can access the first item as list[0]

And

result.length - 1; // Subtract 1 from length of list

Code after applying the change:

dialogRef.afterClosed().subscribe(result => {
  if(result != '' && result != undefined && result != null){
    for(var i = 0; i <= result.length - 1; i++){
      this.imagesArray.push(result[i]);
    }
  } 
});

You can do this without subtracting the -1 from the length, just change the condition parameter to i < result.length as:

dialogRef.afterClosed().subscribe(result => {
  if(result != '' && result != undefined && result != null){
    for(var i = 0; i < result.length; i++){
      this.imagesArray.push(result[i]);
    }
  } 
});

StackBlitz Example

You're initializing your loop variable, i, to 1, result should have a 0 based index, meaning that you should make i = 0, and use less than (<) instead of less than or equal (<=)

That's why you're getting undefined as the last object added to the array

imagesArray : UploadedImages[] = [];

dialogRef.afterClosed().subscribe(result => {
  if(result != '' && result != undefined && result != null){
    for(var i = 0; i < result.length ; i++){
      this.imagesArray.push(result[i]);
    }
  } 
});

There is a undefined value in your array after pushing all values. set array empty in each subscribe and then push to avoid null values.

dialogRef.afterClosed().subscribe(result => {
  this.imagesArray = [];
  if(result != '' && result != undefined && result != null){
    for(var i = 0; i <= result.length ; i++){
      this.imagesArray.push(result[i]);
    }
  } 
});

Also use the optional operator to double check whether the array element is not undefinec.

<b>{{images?.imageName}}</b> 
<b>{{images?.filesize}}</b>
发布评论

评论列表(0)

  1. 暂无评论