lunes, 8 de marzo de 2021

Converting a string to an array REACT

If you had a comma-separated list, you could split it into an array like this.

var shoppingList = 'apples,pears,cookies,bread';

// Returns ["apples", "pears", "cookies", "bread"]

var shoppingListArray = shoppingList.split(',');

console.log( 

                        objectStringArray 

                 )

If you had a comma-separated array, you could split it into an array like this.


var objectstring  ='{Header: "ProductNum2", accessor:"A1", filter:false, orderBy:false },{ Header: "Name", accessor:"A2", filter:false, orderBy:false },{ Header: "Description", accessor:"A3", filter:false, orderBy:false },{ Header: "TxnQty", accessor:"TxnQty", filter:false, orderBy:false },{ Header: "UOM", accessor:"UOM", filter:false, orderBy:false },{ Header: "TxnQtyUOMConvFactor", accessor:"TxnQtyUOMConvFactor", filter:false, orderBy:false },{ Header: "TxnQtyUom", accessor:"TxnQtyUom", filter:false, orderBy:false },{ Header: "Value", accessor:"Value", filter:false, orderBy:false }' 

var objectStringArray = (new Function("return [" + objectstring+ "];")());


             console.log( 

                objectStringArray 

             )   

  

source:

 https://stackoverflow.com/questions/13272406/convert-string-with-commas-to-array

 https://gomakethings.com/converting-a-string-to-an-array-with-vanilla-javascript/