0

I'm trying to inject an array of one key objects into a hidden input for a form so that when I submit the form, it returns the contents of the array in a way I can spread into a new array using .split. However, when I check the values of the input, it shows the contents as [object Object], [object Object], ... even though the actual array may look like [{test: 1}, {test2: 1}, ...]. How do I get the objects to essentially spread into the value for the input?

  var sprints = [];

  function newSprint() {
    var li = document.createElement("li");
    li.className = "color1";
    li.setAttribute("onclick", "this.classList.toggle('checked')")
    var inputValue = document.getElementById("sprints-input").value;
    li.innerHTML = inputValue
    if (inputValue === '') {
      alert("You must write something!");
    } else {
      document.getElementById("sUL").appendChild(li);
      var sprintInput = document.getElementById("sprints");
      li.setAttribute("data-name", inputValue);
      sprints.push({[inputValue]: 1});
      sprintInput.value = sprints;
    }

the sprintInput is where I'm trying to inject my array of objects, sprints. I've also tried ... to spread it but it doesn't seem to work in browser code as it reffers to it as an unrecognized token.

  • Does this answer your question? [What does \[object Object\] mean?](https://stackoverflow.com/questions/4750225/what-does-object-object-mean) – yqlim Oct 08 '20 at 16:46
  • check [this answer](https://stackoverflow.com/a/25419538/6532549) – yqlim Oct 08 '20 at 16:47
  • Not sure I understood exactly what you need. But if you want to covert your array with object to a string you can do JSON.stringify(sprints) – Liron Oct 08 '20 at 16:59
  • Ahhh strinfigying it did the trick! Just need the objects spread when I inject the array so that I can pick them up on the NodeJS side and load them into a SQL DB. Thanks! – Dereck Bearsong Oct 08 '20 at 18:12

1 Answers1

0

What Liron suggested worked: JSON.stringify converted the json object into a string that could then be injected into the value in an input field in HTML. Was doing this so I could then bring that data over into NodeJS and load that as an object.