[英]How to get key from json in angular
How to get key "employee "如何获得关键的“员工”
{ employee":{"name":"John","age":12} {员工“:{“姓名”:“约翰”,“年龄”:12}
} }
It's not about Angular, this is plain Javascript:这与 Angular 无关,这是纯 Javascript:
const obj = { employee:{name:"John",age:12}}
const key = Object.keys(obj)[0]
You can use forEach to get your output if you use Array Object如果您使用Array Object ,则可以使用forEach获取输出
function example1(array){ let res = []; array = [{"ex1":{"name":"John1","age":12}}, {"ex2":{"name":"John2","age":12}}, {"ex3":{"name":"John3","age":12}}] array.forEach((e, idx) => { res.push(Object.keys(e)) }) document.getElementById("ex1").innerHTML = JSON.stringify(res); }
<input type="button" value="example1" onclick="example1()"> <p id="ex1"></p>
if you just use only object you can use For...in to get your output如果您只使用对象,您可以使用For...in来获取输出
function example2(array){ let res = []; obj = {"ex1":{"name":"John1","age":12}, "ex2":{"name":"John2","age":12}, "ex3":{"name":"John3","age":12}} for(var val in obj){ res.push(val) } document.getElementById("ex2").innerHTML = JSON.stringify(res); }
<input type="button" value="example2" onclick="example2()"> <p id="ex2"></p>
But remember when you put object or array or whatever check its valid or not.但请记住,当您放置对象或数组或任何检查其有效与否的东西时。 In your object is invalid here is the correct format
在您的对象无效这里是正确的格式
{"employee":{"name":"John","age":12}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.