Home > OS >  Converting array of strings into array of objects in React JS
Converting array of strings into array of objects in React JS

Time:03-11

I have an array as,

const headArray = ['id', 'correlationId', 'category', 'subCategory'];

I want to convert this as below,

    const columns =  [{ field: 'id', headerName: 'id'},
                      { field: 'correlationId', headerName: 'correlationId'},
                      { field: 'category', headerName: 'category'}
                      { field: 'subcategory', headerName: 'subcategory'}];

I tried many ways but I couldn't do this. If anyone know the method please tell me.

CodePudding user response:

I guess you can use map to achieve this :

const columns = headArray.map(value => ({field: value, headerName: value}));

  • Related