访问JSON对象数组中的单个数组元素

时间:2020-12-20 21:29:44

I have an array of JSON objects - each key has multiple values in an array like this:

我有一个JSON对象数组——每个键在数组中都有多个值:

{
  "1": [
    "hi",
    "hello"
  ]
}

How to I access individual elements "hi" and "hello"?

如何访问单个元素“hi”和“hello”?

I have tried below but doesn't work

我在下面试过了,但是没用

//suppose var imdb = [] contains the above array of JSON object
for (var i = 0; i < imdb.length; i++) {
  var object = imdb[i];
  var strArray = JSON.stringify(object, null, 2);
  console.log(strArray) //prints the above
  console.log(imdb[1][0]) // throws error, was expecting to print "hi"
}

1 个解决方案

#1


2  

You can do it this way -

你可以这样做。

let data = [
  {
  "1": [
    "hi",
    "hello"
    ]
  },

  {
   "2": [
     "hi again",
     "hello again"
    ]
  }
]

function foo(data) {
  data.forEach(obj => {
    for(let prop in obj) {
      obj[prop].forEach(x => console.log(x));
    }
  })
}


foo(data);

What this does is -

这是-

  1. Iterate through the outermost array
  2. 遍历最外层数组
  3. Get the key for the object (which is the current element of the array being iterated upon)
  4. 获取对象的键(它是正在迭代的数组的当前元素)
  5. Iterate through the value of this key (which is an array), and print each item.
  6. 遍历这个键(它是一个数组)的值,并打印每个项。

You can use JSON.parse() to get the correct representation of data from your original JSON response.

可以使用JSON.parse()从原始JSON响应中获得数据的正确表示。


For your original code, if you want to print the array values at a specific position, like "hi" from the first object, and "hi again" from the second object, here's the modified code -

对于原始代码,如果您想在特定位置打印数组值,比如第一个对象的“hi”和第二个对象的“hi”,下面是修改后的代码-

function bar(imdb) {
   // parse imdb first, then -
   for (var i = 0; i < imdb.length; i++) {
     var object = imdb[i];
     var objectKey = Object.keys(object)[0];
     console.log(object[objectKey][0]);
  }
}

Object.keys(object)[0] would return the key of the current object you're iterating upon ("1" or "2"), and using object[objectKey][0], you can print the element at the 0th index of the key's value.

object .keys(object)[0]将返回当前对象的键(“1”或“2”),并使用对象[objectKey][0],您可以在键值的第0个索引处打印元素。

#1


2  

You can do it this way -

你可以这样做。

let data = [
  {
  "1": [
    "hi",
    "hello"
    ]
  },

  {
   "2": [
     "hi again",
     "hello again"
    ]
  }
]

function foo(data) {
  data.forEach(obj => {
    for(let prop in obj) {
      obj[prop].forEach(x => console.log(x));
    }
  })
}


foo(data);

What this does is -

这是-

  1. Iterate through the outermost array
  2. 遍历最外层数组
  3. Get the key for the object (which is the current element of the array being iterated upon)
  4. 获取对象的键(它是正在迭代的数组的当前元素)
  5. Iterate through the value of this key (which is an array), and print each item.
  6. 遍历这个键(它是一个数组)的值,并打印每个项。

You can use JSON.parse() to get the correct representation of data from your original JSON response.

可以使用JSON.parse()从原始JSON响应中获得数据的正确表示。


For your original code, if you want to print the array values at a specific position, like "hi" from the first object, and "hi again" from the second object, here's the modified code -

对于原始代码,如果您想在特定位置打印数组值,比如第一个对象的“hi”和第二个对象的“hi”,下面是修改后的代码-

function bar(imdb) {
   // parse imdb first, then -
   for (var i = 0; i < imdb.length; i++) {
     var object = imdb[i];
     var objectKey = Object.keys(object)[0];
     console.log(object[objectKey][0]);
  }
}

Object.keys(object)[0] would return the key of the current object you're iterating upon ("1" or "2"), and using object[objectKey][0], you can print the element at the 0th index of the key's value.

object .keys(object)[0]将返回当前对象的键(“1”或“2”),并使用对象[objectKey][0],您可以在键值的第0个索引处打印元素。