访问数组内另一个对象内的对象

时间:2018-12-04 18:55:08

标签: javascript reactjs

我有一个对象数组,其中每个对象内都有另一个数组。我需要访问该数组内的对象。我该怎么办?

作为一个示例,这是我的函数,在该函数中,我将其中每个阵列登录到控制台。我想改为控制台记录每个描述。

  const var = data.filter((u) => { 
       console.log(u.array)
    })

这是JSON数据

[
  {
    "agreed": true,
    "email": "test@test.com"
    "array": [
      {
        "name": "Alex",
        "city": "Pedro",
        "state": "WA",
        "description": "Alex was very patient. He is one of the good guys!"
      }
    ]
  }
]

5 个答案:

答案 0 :(得分:1)

如果您知道确切的索引,则可以执行此操作。

const var = data.filter((u) => { 
       console.log(u.array[0].description)
    })

如果您不知道确切的索引,或者想对数组中的每个项目执行此操作,则可以这样做。

const var = data.filter((u) => { 
  u.array.forEach(item => {
    console.log(item.description)
  })
})

答案 1 :(得分:1)

这是代码段。数据包含原始数组,则u包含外部数组的每个对象。然后,u.array.map遍历每个单独的数组,而i.description包含每个子数组的描述。

data.map((u) => {
    u.array.map((i) => {
        console.log(i.description);
    }
})

答案 2 :(得分:0)

对于第一项,您以array[someIndex]索引到数组中,并且以0开头。

因此您可以:

let arr = [{
  "agreed": true,
  "email": "test@test.com",
  "array": [{
    "name": "Alex",
    "city": "Pedro",
    "state": "WA",
    "description": "Alex was very patient. He is one of the good guys!"
  }]
}]

// get the first whole object
console.log(arr[0])

// get the arra property of the first object
console.log(arr[0].array)

// get the first object of that array
console.log(arr[0].array[0])

// get a property on that object
console.log(arr[0].array[0].name)

如果您需要深入研究数组并访问操纵值,则可以使用forEachreduce()等工具在它们上循环:

let arr = [{"agreed": true,"email": "test@test.com","array": [{"name": "Alex","city": "Pedro","state": "WA","description": "Alex was very patient. He is one of the good guys!"},{"name": "Mark","city": "Anchorage","state": "AK","description": "Mark is also patient. He is one of the good guys!"}]},{"agreed": true,"email": "test@test.com","array": [{"name": "Steve","city": "New York","state": "NY","description": "Steve was very patient. He is one of the good guys!"},{"name": "Nancy","city": "Anchorage","state": "AK","description": "Nancy is also patient. She is awesome!"}]}]

// log each name
arr.forEach(obj => {
    obj.array.forEach(item => {
        console.log(item.name)
    })
})

// build a new list of just the cities
let cities = arr.reduce((arr, obj) => {
    obj.array.forEach(item => {
        arr.push(item.city)
    })
    return arr
}, [])
console.log(cities)

答案 3 :(得分:0)

好吧

如果这将是您的Javascript对象的结构

var data =    
[
  {
    "agreed": true,
    "email": "test@test.com"
    "array": [
      {
        "name": "Alex",
        "city": "Pedro",
        "state": "WA",
        "description": "Alex was very patient. He is one of the good guys!"
      }
    ]
  }
]

然后您可以通过以下方式访问阵列

data[0].array[0].name;

如果您使用的是jquery,则可以这样console.log描述

$.each(data[0].array, function(i,v){
   console.log(v.description);
})

答案 4 :(得分:0)

您可以将所有描述保存到数组中或仅显示它,就像这样

let descriptions = [];
data.map(item => item.array)
    .forEach(array => {
        array.forEach(obj => {
            console.log(obj.description);
            descriptions.push(obj.description);
        });
    });

console.log(descriptions);