Javascript Angular 2 使用动态键从 json 数组中获取键和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45398668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:02:45  来源:igfitidea点击:

Angular 2 get key and value from json array with dynamic key

javascriptangulartypescript

提问by Batsheva

I want to get key and value from json array with dynamic keys. Meaning, I don't know in advance what will be the keys.

我想使用动态键从 json 数组中获取键和值。意思是,我事先不知道密钥是什么。

This is an example to the json the function gets:

这是函数获取的 json 示例:

arr = [
       {key1: 'val1'},
       {key2: 'val2'},
       {key3: 'val3'}
      ];

It seems simple to me but I am unable to get the key and value for each item.

这对我来说似乎很简单,但我无法获得每个项目的键和值。

This is what I tried (based on this pipe):

这是我尝试过的(基于此管道):

for (let key of arr) {
   console.log ('key: ' +  key + ',  value: ' + arr[key]);
 }

But what I get in the log is the following:

但是我在日志中得到的是以下内容:

key:[object Object], value: undefined

My expected behavior is to get the following:

我的预期行为是获得以下结果:

key:key1, value:val1

What am I doing wrong? How can I get the keys and values?

我究竟做错了什么?我怎样才能得到键和值?

回答by Hendrik Brummermann

In your example, you have an array of objects and each of these object has exactly one property.

在您的示例中,您有一个对象数组,并且这些对象中的每一个都只有一个属性。

for (let obj of arr) {
    console.log("object:", obj);
    for (let key in obj) {
        console.log("      key:", key, "value:", obj[key]);
    }
}

The following code from your posting

以下代码来自您的帖子

for (let key in arr) {
    console.log ('key: ' +  key + ',  value: ' + arr[key]);
}

... would work on a data structure like this:

...将在这样的数据结构上工作:

let arr = {
    key1: 'val1',
    key2: 'val2',
    key3: 'val3'
};

回答by Sachin Mishra

If you are more concerned to specify object like

如果您更关心指定对象,例如

var temp={'name':Dinesh,'age':18}

You may use following syntax.

您可以使用以下语法。

console.log(Object.keys(temp)[0],Object.values(temp)[0]):

Specially zero index because object's both method keys and values return an array

特别是零索引,因为对象的方法键和值都返回一个数组

回答by Sajeetharan

You need another for loop to access key and value,

您需要另一个 for 循环来访问键和值,

for (let key of this.arr) {
 for(var i in key){
      console.log('key: ' +  i + ',  value: ' + key[i]);
 }
}

Check the console

检查 console

DEMO

演示

回答by reihaneh

yourObject.forEach(function(value, index){
   // do your job
   console.log(value, index);
});

回答by digvijay deshmukh

You can try these

你可以试试这些

object = [
{
  "id": 1,
  "test": "test1",
  "name": ["abc", "pqr"]

},
{
  "id": 2,
  "test": "test2",
  "name": ["abc2", "pqr2"]

},
{
  "id": 3,
  "test": "test3",
  "name": ["abc3", "pqr3"]

},
{
  "id": 4,
  "test": "test4",
  "name": ["abc4", "pqr4"]

}]

and your js or typescript code be like:-

并且您的 js 或打字稿代码如下:-

YourMethod() {
for (let item of this.object) {
  for (let i in item) {
    if (typeof (item[i]) === 'object') {
      item[i].forEach((e: any) => {
        console.log("runseprate", e)
      })
    }
  }
}}

回答by Techdive

   json = {payload.InsuredMobileNumber.type: "string"
payload.accidentTime.type: "string"
payload.causeOfLoss.type: "string"
payload.claimRegistrationDate.type: "string"
payload.emailAddress.type: "string"}


 var keyData = Object.keys(json);
          var valueData = Object.values(json)
          console.log("keyData = ", keyData)
          console.log("valueData = ", valueData)