如何动态将值添加到json对象 [英] How can I add values to a json object dynamically

查看:76
本文介绍了如何动态将值添加到json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON对象,我想向其中添加用户输入的值.我正在学习有角度的知识,我需要做到有角度的知识.我看了很多,但没有看到任何方法可以轻松地做到这一点.我只想知道如何向json对象添加一对数据
这是我在打字稿文件中的代码.

I have a json object to which i want to add values that user enters. I am learning angular and i need to do this in angular. I looked a lot but did not see any method to do this easily. I only want to know how to add a pair of data to the json object
This is my code in typescript file.

 people: any[]=[{
          "name": "jacob",
          "age": 22 },
          {
          "name": "sruthi",
          "age": 29 }
      
  ]

我想在用户输入不同的名称和年龄时插入值.
这是我的html代码

I want to insert values as the user enters different names and age.
This is my html code

  <input type="text" class="frm-control" placeholder="Enter Name" (change)="insert_name($event.target.value)"> &nbsp;
  <input type="text" class="frm-control" placeholder="Enter Age" (change)="insert_age($event.target.value)">
  <br>
  <button type="button" class="btn btn-success" style="margin-top:10px;" (onclick)="submit()">Submit</button>

推荐答案

您可以使用push()方法

You can do it with the push() method

this.people.push({name:this.name, age:this.age})

这篇关于如何动态将值添加到json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆