Having an issue populating an array based on another array where pushing a value onto a specific index seems to populate all indexes.
Code
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
public matrix: number[] = [
// Array values here
];
public matrixColumns: number[][] = [];
public expectedMatrixColumns: number[][] = [
// Nested arrays here
];
public numberofColumns: number = 9;
columnStartIndex: number = 0;
constructor() {
this.createColumnMatrix();
}
createColumnMatrix() {
// Logic for creating column matrix here
}
}
Demo Project
JavaScript version:
class AppComponent {
matrix = [
// Array values here
];
matrixColumns = [];
expectedMatrixColumns = [
// Nested arrays here
];
numberofColumns = 9;
columnStartIndex = 0;
constructor() {
this.createColumnMatrix();
}
createColumnMatrix() {
// Logic for creating column matrix here
}
}
let component = new AppComponent();
for (let row of component.matrixColumns) {
console.log(JSON.stringify(row));
}
Problem
Issue lies within this line:
this.matrixColumns[columnIndex].push(this.matrix[matrixIndex]);
The first value in the 'matrix' array is being pushed into every index in 'matrixColumns' instead of following the expected output pattern.
Expected output
// Expected output representation here
Actual output
// Actual output representation here