1

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

adiga
  • 34,372
  • 9
  • 61
  • 83
Pelumi
  • 413
  • 2
  • 5
  • 13

2 Answers2

1

You can do something like this using JSON.parse. I have changed the name of the objectResult keys and made it similar to what typeof returns so that it's easier to sort them into different categories using [] brackets. You can use a switch instead if you want custom keys like Others

function sort() {
  const numbers = [];
  const strings = [];
  const booleans = [];
  const others = [];
  const value = document.querySelectorAll(".input")[0].value;
  let inputArray

  try {
    inputArray = JSON.parse(value)
  } catch {
    alert("Please enter a valid JSON string")
    return;
  }

  const objectResult = {
    "string": strings,
    "number": numbers,
    "boolean": booleans,
  };

  inputArray.forEach(item => {
    objectResult[typeof item].push(item)
  });

  const JsonObj = JSON.stringify(objectResult)
  document.querySelector("#rawResult").innerText = JsonObj;
}
<div class="inputs">
  <input class="input" placeholder="Enter an Array" value='[1,"bad",false,3,"key"]' />
  <button onclick="sort()">SORT ARRAY</button>
</div>

<div class="result">
  <div id="rawResult"></div>
  <div id="tableResult"></div>
</div>
adiga
  • 34,372
  • 9
  • 61
  • 83
0

You can't do by typeof , you can do by some simple method like isNaN, for boolean I think you need to check exact string for true or false or other will string ...

if(!isNaN(inpArr[i].value)) { //Number
  numbers.push(inpArr[i].value);
} else if(inpArr[i].value == "true" || inpArr[i].value == "false") { // boolean
   booleans.push(inpArr[i].value);
} else { // String
   strings.push(inpArr[i].value);
}
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52