0

From my API I get back 2 arrays, one with the column names and one with the data matching those columns.
To use ag-grid I need to match the columns to the properties of a class.

eg: a column call foo and a class with a property called foo:

  public columnDefs: Column[] =
    [{ field: 'foo', headerName: 'Foo') }];

then I would need a class to match that:

export class Bar { public foo: string; }

Now, the problem, I get back one array with the names of the column and another with the matching data, so I need to create something like this:

x[0] are the columns that need to be properties, x[1] are the values for that property

    for (let index = 0; index < x[1].length; index++) {
              this.rowData.push({ x[0][index]: x[1][index] });
            }

How can this be done?

Hypenate
  • 1,907
  • 3
  • 22
  • 38

1 Answers1

2

You need to actually get the value of x[0][index] as a key name so you can use ES6 approach of enclosing that in [] like

this.rowData.push({ [x[0][index]]: x[1][index] });

var x = [['someKey'],['someValue']];
var rowData = [], index = 0;
rowData.push({ [x[0][index]]: x[1][index] });
console.log(rowData);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • 1
    This has been repeated asked and answered. No need for Yet Another Answer. – T.J. Crowder Aug 27 '18 at 12:36
  • @T.J.Crowder Sorry, I didn't know the search terms it seemed – Hypenate Aug 27 '18 at 12:48
  • @Hypenate - It's no biggie (and some duplicate questions can be a good thing, helping people find the one canonical answer), but some users are happy to constantly re-answer answered questions, which is actively the opposite of how SO is supposed to work. (I suggest un-accepting the answer, so Ankit has the option of deleting it. [He can't delete an accepted answer.]) – T.J. Crowder Aug 27 '18 at 12:50
  • 1
    @T.J.Crowder don't take it personal but there are several times I see you answering the duplicate questions but we actually cannot say that duplicate because the OP has different case or context there. The way OP present the question and the scenario they use it also differ. – Ankit Agarwal Aug 27 '18 at 12:54
  • I have found many such people where they say don't answer duplicates but they themselves keep answering duplicates. – Ankit Agarwal Aug 27 '18 at 12:55
  • @T.J.Crowder Nothing personal with you. I have always liked your answers and respect your comments. I am ready to delete this. – Ankit Agarwal Aug 27 '18 at 12:56