简体   繁体   English

Angular 6 / Typescript 从 Json 对象中获取键名

[英]Angular 6 / Typescript Get key names from Json object

I have some data which I'm getting from jsonplaceholder and I want to add it to a table.我有一些从 jsonplaceholder 获取的数据,我想将其添加到表中。

The data looks like this:数据如下所示:

[{
    userId: 1,
    id: 1,
    title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    body: "quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto"
  }
]

The first thing I need to do for the table as to popular the我需要为桌子做的第一件事是流行

But in order to do that I need to be able to get the key names and add it to another object so that I can dynamically popular the html table heading.但是为了做到这一点,我需要能够获取键名并将其添加到另一个对象中,以便我可以动态地流行 html 表标题。

My question is how can I create a list of keys from an object?我的问题是如何从对象创建键列表?

Object.keys(obj) is what you're looking for. Object.keys(obj)就是你要找的。 It returns an array of keys.它返回一个键数组。

You can also loop over keys with :您还可以使用以下方法循环键:

for(let key in obj) {
    // Whatever you want to do with key or obj[key]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM