I'm trying to let user input an array in an <input>
. Then I would sort them into different arrays based on their data types; (number, strings and booleans).
But I cant figure out how to loop through the input to sort them out. Example input is : [1,"bad",false,3,"key"]
EXTRA: If you can help complete code. This the source code :Codepen
function sort() {
const numbers = [];
const strings = [];
const booleans = [];
const others = [];
const inpArr = document.querySelectorAll(".input");
//console.log(inpArr.value);
for (let i = 0; i < inpArr.length; i++) {
console.log(inpArr[i].value);
switch (typeof inpArr[i].value) {
case "Number":
numbers.push(inpArr[i].value);
break;
default:
console.log(typeof inpArr[i].value)
}
}
const objectResult = {
"Strings": strings,
"Numbers": numbers,
"Booleans": booleans,
"Others": others
};
const JsonObj = JSON.stringify(objectResult)
document.querySelector("#rawResult").innerText = JsonObj;
}
<div class="inputs">
<input class="input" placeholder="Enter an Array" />
<button onclick="sort()">SORT ARRAY</button>
</div>
<div class="result">
<div id="rawResult"></div>
<div id="tableResult"></div>
</div>
I need how to loop through the inpArr and sort each values into their respective arrays. and show the user as object