Building a new array from a filterered and keying it

Viewed 32

I have an array that looks similar to this,

[
    [
        "Show/Hide",
        "P000",
        "MAX-CT05 FVM2-",
        "S",
        1532,
        -9.5929406005,
        null,
        null,
        null,
        "Open",
        null,
        "",
        null
    ],
    [
        "",
        "P000-000",
        "MAX-CT05 FVM2-",
        "S",
        22,
        -9.985,
        null,
        null,
        null,
        "Open",
        null,
        null,
        "P000"
    ],
    [
        "",
        "P000-002",
        "MAX-CT05 FVM2-",
        "S",
        250,
        -9.874,
        null,
        null,
        null,
        "Open",
        null,
        null,
        "P000"
    ],
    [
        "",
        "P000-003",
        "MAX-CT05 FVM2-",
        "S",
        1,
        -9.945,
        null,
        null,
        null,
        "Open",
        null,
        null,
        "P000"
    ],
    [
        "",
        "P000-001",
        "MAX-CT05 FVM2-",
        "S",
        1259,
        -9.53,
        45,
        null,
        null,
        "Open",
        null,
        null,
        "P000"
    ],
    [
        "Show/Hide",
        "P001",
        "MAX-CT07 TYM2-",
        "S",
        333,
        -1.8855675676,
        null,
        null,
        null,
        "Open",
        null,
        null,
        null
    ],
    [
        "",
        "P001-000",
        "MAX-CT07 TYM2-",
        "S",
        99,
        -1.865,
        null,
        null,
        null,
        "Open",
        null,
        "98.8mm JPM PB",
        "P001"
    ],
    [
        "",
        "P001-001",
        "MAX-CT07 TYM2-",
        "S",
        45,
        -1.866,
        null,
        null,
        null,
        "Open",
        null,
        null,
        "P001"
    ],
    [
        "",
        "P001-002",
        "MAX-CT07 TYM2-",
        "S",
        189,
        -1.901,
        7,
        null,
        -1.901,
        "Open",
        null,
        null,
        "P001"
    ]
    
]

I have another array that looks like this,

[
    {
        "header": true
    },
    {
        "pk": "P000",
        "nest": 0
    },
    {
        "pk": "P000-000",
        "nest": 1
    },
    {
        "pk": "P000-002",
        "nest": 1
    },
    {
        "pk": "P000-003",
        "nest": 1
    },
    {
        "pk": "P000-001",
        "nest": 1
    },
    {
        "pk": "P001",
        "nest": 0
    },
    {
        "pk": "P001-000",
        "nest": 1
    },
    {
        "pk": "P001-001",
        "nest": 1
    },
    {
        "pk": "P001-002",
        "nest": 1
    }
]

This array basically outlines parent child structure, if the nest value is zero then then the array at the index in the other array is parent, if the nest value is 1 then the array at that index is a child, what I wanting to do is loop over the second array and create a new array of grouped children indexed by their parents index, something like,

[
    0: [
        [
        "",
        "P000-000",
        "MAX-CT05 FVM2-",
        "S",
        22,
        -9.985,
        null,
        null,
        null,
        "Open",
        null,
        null,
        "P000"
    ],
    [
        "",
        "P000-002",
        "MAX-CT05 FVM2-",
        "S",
        250,
        -9.874,
        null,
        null,
        null,
        "Open",
        null,
        null,
        "P000"
    ],
    [
        "",
        "P000-003",
        "MAX-CT05 FVM2-",
        "S",
        1,
        -9.945,
        null,
        null,
        null,
        "Open",
        null,
        null,
        "P000"
    ],
    [
        "",
        "P000-001",
        "MAX-CT05 FVM2-",
        "S",
        1259,
        -9.53,
        45,
        null,
        null,
        "Open",
        null,
        null,
        "P000"
    ]
    ],
    1: [
       [
        "",
        "P001-000",
        "MAX-CT07 TYM2-",
        "S",
        99,
        -1.865,
        null,
        null,
        null,
        "Open",
        null,
        "98.8mm JPM PB",
        "P001"
    ],
    [
        "",
        "P001-001",
        "MAX-CT07 TYM2-",
        "S",
        45,
        -1.866,
        null,
        null,
        null,
        "Open",
        null,
        null,
        "P001"
    ],
    [
        "",
        "P001-002",
        "MAX-CT07 TYM2-",
        "S",
        189,
        -1.901,
        7,
        null,
        -1.901,
        "Open",
        null,
        null,
        "P001"
    ]
  ]
]

I realise this above is not a valid format I don't think, currently this is what my attempt looks like,

let parentIndex = -1;
this.current.row_meta_data.filter((row: any, index: number) => {
  if(!row.header) {
    if(row.nest == 0) {
      this.current.childRows[`parent_${index}]`] = [];
      parentIndex = index;
    }
    debugger;
    if(row.nest == 1) {
      this.current.childRows[`parent_${index}`].push({
        values: this.current.values[index],
        cell_styles: this.current.cell_styles[index],
        formatted_values: this.current.formatted_values[index],
        unique_styles: this.current.unique_styles[index]
      });
    }
  }
});

But I am getting an error

Cannot set properties of undefined (setting 'parent_1]')

1 Answers

I am not really sure of the variables that you have but can you add this.current.childRows to both if conditions? for example:

if (row.nest == 0 && this.current.childRows)

and also same for the second condition

Related