var string = ":All;true:Yes;false: "; var array = string.split(/:|;/); var listItems = []; for (var i = 0; i < array.length; i += 2 ) { listItems.push({itemValue: array[i], itemText: array[i + 1]}) }
Please note that the string will be set to false and true. The same goes for numbers, if you have one. If you want to save them with the appropriate types, you need to add a manual conversion. Something like:
function value(val) { if (!isNaN(val)) return +val; if (val === "true") return true; if (val === "false") return false; return val; }
Therefore, the string that clicks the object on the array will change as follows:
listItems.push({itemValue: value(array[i]), itemText: array[i + 1]})
Zer0
source share