【问题标题】:Access properties of objects into nested Array - Javascript将对象的属性访问到嵌套数组中 - Javascript
【发布时间】:2020-07-13 06:31:13
【问题描述】:

我想从以下数组中检索所有文本(文本 1、文本 2....):

[
    {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 1"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 2"
      }
    ]
  },
  {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 3"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 4"
      }
    ]
  },
  {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 5"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 6"
      }
    ]
  }
]  

这个数组存储在一个名为stores 的变量中。

我尝试了以下方法:

const listText = stores.map(count => count.reviews.text // [null, null, null]
const listText = stores.map((count, i) => count.reviews[i].text) // 无法读取 undefined 的属性 'text'
const listText = stores.forEach((key, i) => key.reviews[i].text) // 无法读取 undefined 的属性 'text'

你能帮帮我吗? 非常感谢

【问题讨论】:

    标签: javascript arrays loops object nested


    【解决方案1】:

    你不能使用count.reviews.text,因为reviews是一个数组,所以你还应该遍历reviews:

    const data = [
    {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 1"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 2"
      }
    ]
      },
      {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 3"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 4"
      }
    ]
      },
      {
    "reviews": 
    [
      {
        "_id": "5e84239d6e24358b50f3fe4e",
        "text": "My text 5"
      },
      {
        "_id": "5e8423a46e24358b50f3fe4f",
        "text": "My text 6"
      }
    ]
      }
    ]  
    
    const listText = data.reduce((acc,current) => acc.concat(current.reviews.map(it => it.text)), [])
    
    console.log(listText)

    【讨论】:

    • 是的,它工作得很好:) 谢谢。如果我只想从数组中选择一些值而不是其他值呢?在我的示例中,我只有_idtext。但是假设我还有其他属性,例如 photoslug.... 我怎样才能确保只选择 _idtext 而不是其他属性?
    • 能否请您澄清所需的输出?如果您有一个包含很多字段的对象数组,并且您需要将其转换为只有 2 个字段的对象列表?例如const initialArr = [{id:1, text:'abc', rank:5, etc:'some vale'}, {id:2, text:'rwr', rank:3, etc:'some vale'}, {id:3, text:'36y3563', rank:6, etc:'some vale'}]你可以使用.map函数:initialArray.map(item=> ({id: item.id, text:item.text}))
    • 这正是我所需要的。你对 Elivra 很有帮助,非常感谢!
    【解决方案2】:

    地图和平面:

    const data = [ 
    { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 1" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 2" } ] }, 
    { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 3" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 4" } ] }, 
    { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 5" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 6" } ] } ] 
    console.log(
      data.map(items => items.reviews.map(review => review.text)).flat()
    )

    【讨论】:

    • 不使用.flat() 它可以工作,即使我将数组放入数组。但是当使用.flat() 时,我会登录flat is not a function。谢谢@mplungjan
    【解决方案3】:

    在下面试试这个:

    let output = [];
                input.map(function (item_1) {
                    item_1.reviews.map(function (item_2) {
                      output.push(item_2.text);
                    })
                });
    

    【讨论】:

    • 谢谢@Abhishek Kulkarni :)
    【解决方案4】:

    使用.flatMap(),您可以遍历每个对象,然后.map() 将每个对象的reviews 数组复制到包含文本属性的数组中。 .flatMap() 允许您将所有内部映射数组结果展平到一个更大的结果数组中。

    const stores = [ { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 1" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 2" } ] }, { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 3" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 4" } ] }, { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 5" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 6" } ] } ];
    
    const res = stores.flatMap((o) => o.reviews.map(({text}) => text));
    console.log(res);

    如果你不能支持.flatMap(),你总是可以使用.map()映射到一个数组数组,然后在使用.concat()spread syntax...)之后将其展平:

    const stores = [ { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 1" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 2" } ] }, { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 3" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 4" } ] }, { "reviews": [ { "_id": "5e84239d6e24358b50f3fe4e", "text": "My text 5" }, { "_id": "5e8423a46e24358b50f3fe4f", "text": "My text 6" } ] } ];
    
    const res = [].concat(...stores.map((o) => o.reviews.map(({text}) => text)));
    console.log(res);

    【讨论】:

    • 完美,完美运行@Nick Parsons。你让我很开心:) 很好的解释
    猜你喜欢
    • 1970-01-01
    • 2016-09-14
    • 2012-11-12
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多