1

I need to create one matrix table of each combination by comparing the two array object using Angular. I am explaining my code below.

ColumnNames= ["Colour", "Size"];
configArr={
  "Colour":["Black", "Red", "Blue","Grey"],
  "size": ["XS","S","XL","XXL","M"]
}

Here I have two array. My first array ColumnNames values will be the table column header and accordingly I need to create the matrix like below.

Expected output::

Colour        Size

 Black          XS
 Black           S
 Black          XL
 Black         XXL
 Black          M
 Red           XS
 Red            S
 Red           XL
 Red           XXL
 Red            M

.................
................
................

Like the above format I need to display the data into angular table. I need only create the matrix dynamically.

6
  • Do you want this as an html table? Commented Feb 5, 2020 at 8:56
  • @IraklisGkougkousis: yes, first the matrix need to be formed and then I can bind that array inside one table. Commented Feb 5, 2020 at 8:57
  • Another question, do you only want to show the ColumnNames that you have defined, even if configArr has more properties? Example: configArr has Colour, Size, Price. ColumnNames only has Colour and Size -> so you only want to show Colour and Size, correct? Commented Feb 5, 2020 at 9:18
  • There may be more key values in future. Commented Feb 5, 2020 at 9:21
  • I understand that, I'm asking something else. I mean do you only want to show ColumnNames even if configArr has more key values? See my example please. Commented Feb 5, 2020 at 9:27

3 Answers 3

1

A generic solution that works with any configArr, as long as all keys in that object are string[]. Inspired by this.

app.component.ts

import { Component, OnInit } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
  name = "Angular";

  // Comment in/out properties here to see how the table changes
  configArr = {
    Colour: ["Black", "Red", "Blue", "Grey"],
    Size: ["XS", "S", "XL", "XXL", "M"],
    //Price: ["$5", "$10", "$15"],
    //Brand: ["B1", "B2", "B3"]
  };

  // This will be an [] of objects with the same properties as configArr
  matrix;

  allPossibleCases(arr) {
    // Taken almost exactly from the linked code review stack exchange link
  }

  ngOnInit() {
    const configKeys = Object.keys(this.configArr);
    const arrOfarrs = configKeys.map(k => this.configArr[k]);
    const result = this.allPossibleCases(arrOfarrs);
    this.matrix = result.map(r => {
      const props = r.split(' ');
      let obj = {};
      for(let i=0;i<props.length;i++) {
        obj[configKeys[i]] = props[i]
      }
      return obj;
    });
  }
}

app.component.html

<table>
  <thead>
    <tr>
      <th *ngFor="let kv of configArr | keyvalue">{{ kv.key }}</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let elem of matrix">
      <td *ngFor="let kv of elem | keyvalue">{{ kv.value }}</td>
    </tr>
  </tbody>
</table>

Stackblitz.

Also, see this for the algorithm behind it.

Sign up to request clarification or add additional context in comments.

Comments

0

Try like this:

<table>
    <tr>
        <th *ngFor="let heading of ColumnNames"> {{heading}} </th>
    </tr>
    <ng-container *ngFor="let color of configArr.Colour">
        <tr *ngFor="let size of configArr.size">
            <td>{{color}}</td>
            <td>{{size}}</td>
        </tr>
    </ng-container>
</table>

Working Demo

3 Comments

No, it did not map with ColumnNames array. Tomorrow there may be some new value.
Can you explain a bit more "did not map with ColumnNames array"
Ok, Here each value of ColumnNames array are the keys for configArr json object. so if I will add subject to ColumnNames and configArr={ "Colour":["Black", "Red", "Blue","Grey"], "size": ["XS","S","XL","XXL","M"], "subject": ["Math","History"] } then at this time what will happen.
0
configArr.Colour.forEach((color) => {
  configArr.size.forEach((size) =>{
    console.log(color ,"  ", size); // Change this according to your table
  })
})

1 Comment

It should be map with ColumnNames values because there may be more values in future so it need to be dynamic. means the values of ColumnNames array will be the key name of configArr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.