Reputation: 877
Ultimately, I want to create an object:
newData = {
column1: "",
column2: "",
column3: "",
...
columnN: ""
}
column names come from another array of objects, which is essentially a global variable tableColumns:
tableColumns = [
{headerName: "column1", field: "c1", editable: false},
{headerName: "column2", field: "c2", editable: false},
{headerName: "column3", field: "c3", editable: false},
{headerName: "column4", field: "c4", editable: false}
]
Without any hard coding, how can I dynamically create newData via looping through this.tableColumns?
Upvotes: 0
Views: 1095
Reputation: 30739
Loop over tableColumns
and set the value of headerName
key as the key of newData
object:
var tableColumns = [
{headerName: "column1", field: "c1", editable: false},
{headerName: "column2", field: "c2", editable: false},
{headerName: "column3", field: "c3", editable: false},
{headerName: "column4", field: "c4", editable: false}
];
var newData = {};
tableColumns.forEach((col)=>{
newData[col.headerName] = '';
});
console.log(newData);
Upvotes: 1
Reputation: 20875
Loop through your items and dynamically assign properties based on your headerName
column
const tableColumns = [
{headerName: "column1", field: "c1", editable: false},
{headerName: "column2", field: "c2", editable: false},
{headerName: "column3", field: "c3", editable: false},
{headerName: "column4", field: "c4", editable: false}
];
const result = {};
tableColumns.forEach(item => {
result[item.headerName] = item.field;
});
console.log(result);
Upvotes: 0