最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Pushing input values Into a JSON Array - Stack Overflow

matteradmin5PV0评论

I am trying to push some values from the input textbox to the JSON array. Here is what i tried

Code:

<!DOCTYPE html>
<html>
<body>

<input id="studentnumber" placeholder="Student Number"></input>
<input id="status" placeholder="Status"></input>
<button id="bt1" onclick="newUser()">Validate</button>
<p id="demo"></p>

<script type="text/javascript">

//Array
var jsonStr = 
    '{"G11S":[{"StudentNumber":"1","status":"Pass"},{"StudentNumber":"2","status":"Pass"},{"StudentNumber":"3","status":"Pass"}]}';


function newUser(){

    x = document.getElementById("studentnumber").value;

    //Debug
    console.log(x);

    var obj = JSON.parse(jsonStr);
    obj['G11S'].push({"StudentNumber": "4","status": "Member"}); // <~~ What am I Supposed to replace the "4" with and the "Member aswell"

    jsonStr = JSON.stringify(obj);
    console.log(jsonStr);
}


</script>

</body>
</html>

So I am trying to push 'x' into the array by defining a variable (x) to get the input textbox's value:

x = document.getElementById("studentnumber").value;

Trying to push it on line 25:

obj['G11S'].push({"StudentNumber": "4","status": "Member"});

I am trying to push some values from the input textbox to the JSON array. Here is what i tried

Code:

<!DOCTYPE html>
<html>
<body>

<input id="studentnumber" placeholder="Student Number"></input>
<input id="status" placeholder="Status"></input>
<button id="bt1" onclick="newUser()">Validate</button>
<p id="demo"></p>

<script type="text/javascript">

//Array
var jsonStr = 
    '{"G11S":[{"StudentNumber":"1","status":"Pass"},{"StudentNumber":"2","status":"Pass"},{"StudentNumber":"3","status":"Pass"}]}';


function newUser(){

    x = document.getElementById("studentnumber").value;

    //Debug
    console.log(x);

    var obj = JSON.parse(jsonStr);
    obj['G11S'].push({"StudentNumber": "4","status": "Member"}); // <~~ What am I Supposed to replace the "4" with and the "Member aswell"

    jsonStr = JSON.stringify(obj);
    console.log(jsonStr);
}


</script>

</body>
</html>

So I am trying to push 'x' into the array by defining a variable (x) to get the input textbox's value:

x = document.getElementById("studentnumber").value;

Trying to push it on line 25:

obj['G11S'].push({"StudentNumber": "4","status": "Member"});
Share Improve this question edited Mar 1, 2016 at 17:44 Sarantis Tofas 5,1671 gold badge25 silver badges36 bronze badges asked Mar 1, 2016 at 16:48 VancerVancer 851 gold badge3 silver badges11 bronze badges 3
  • Its due to the defined variable ' x ', being placed in the statement that pushes data in at line 25 – Vancer Commented Mar 1, 2016 at 17:03
  • try my answer, is this what you want? If not can you make it a little bit clear of what you need? – Sarantis Tofas Commented Mar 1, 2016 at 17:05
  • @Akis_Tfs **Thank you for your help brother ** works like a charm! – Vancer Commented Mar 1, 2016 at 17:09
Add a ment  | 

1 Answer 1

Reset to default 5

You will have to get the input values and store them in an object variable then push that variable in the JSON array:

function newUser(){
  var user = {};

  user.StudentNumber = document.getElementById("studentnumber").value;
  user.status = document.getElementById("status").value;

  //Debug
  console.log(user);

  var obj = JSON.parse(jsonStr);
  obj['G11S'].push(user);

  jsonStr = JSON.stringify(obj);
  console.log(jsonStr);
}
Post a comment

comment list (0)

  1. No comments so far