【问题标题】:How get value in the object in the array, if array is in the object and the object in the next array?如果数组在对象中并且对象在下一个数组中,如何在数组中的对象中获取值?
【发布时间】:2019-10-26 15:44:34
【问题描述】:

预期效果:迭代数组中的对象,在数组comments 中的对象中获取值示例“电子邮件”。 我想拼写数组和嵌套对象和数组并返回“电子邮件”值。当我得到数组comments 时,我尝试在对象中获取价值电子邮件我有错误email is undefined

let scores =  [
  {
    "userId": 1,
    "id": 1,
    "title": "bbbb",
    "project": "JS",
    "completed": false,
    "comments": [
      {
        "itemId": 1,
        "id": 1,
        "name": "provident id voluptas",
        "email": "Meghan_Littel@rene.us",
        "body": "sdsdsd"
      },
      {
        "itemId": 1,
        "id": 2,
        "name": "provident id voluptas",
        "email": "fdfdfdf_Littel@rene.us",
        "body": "sdsdsd"
      }
    ]
  },
 {
    "userId": 1,
    "id": 2,
    "title": "ggggg",
    "comments": [
      {
        "itemId": 2,
        "id": 1,
        "name": "odio adipisci rerum aut animi",
        "email": "Nikita@garfield.biz",
        "body": "dsdsdsd"
      }
    ]
  }
]


let obj;

for (var key in scores) {
      obj = scores[key];
      console.log(obj.comments); //return objects array
    }

 for (var key in ob) {
      let ob1 = ob[key];
      console.log(ob1[email]); //return email is undefined
    }

【问题讨论】:

    标签: javascript arrays object ecmascript-6 javascript-objects


    【解决方案1】:

    您还需要迭代comments 并获取属性email

    使用for ... of statementdestructuring assignment

    let scores = [{ userId: 1, id: 1, title: "bbbb", project: "JS", completed: false, comments: [{ itemId: 1, id: 1, name: "provident id voluptas", email: "Meghan_Littel@rene.us", body: "sdsdsd" }, { itemId: 1, id: 2, name: "provident id voluptas", email: "fdfdfdf_Littel@rene.us", body: "sdsdsd" }] }, { userId: 1, id: 2, title: "ggggg", comments: [{ itemId: 2, id: 1, name: "odio adipisci rerum aut animi", email: "Nikita@garfield.biz", body: "dsdsdsd" }] }];
    
    for (let { comments } of scores) {
        for (let { email } of comments) {
            console.log(email);
        }
    }

    Array#forEach

    let scores = [{ userId: 1, id: 1, title: "bbbb", project: "JS", completed: false, comments: [{ itemId: 1, id: 1, name: "provident id voluptas", email: "Meghan_Littel@rene.us", body: "sdsdsd" }, { itemId: 1, id: 2, name: "provident id voluptas", email: "fdfdfdf_Littel@rene.us", body: "sdsdsd" }] }, { userId: 1, id: 2, title: "ggggg", comments: [{ itemId: 2, id: 1, name: "odio adipisci rerum aut animi", email: "Nikita@garfield.biz", body: "dsdsdsd" }] }];
    
    scores.forEach(({ comments }) => comments.forEach(({ email }) => console.log(email)));

    【讨论】:

    • 如何拉一些没有数组的对象。我有结果comments:[{},{},{}]。我想要结果 {},{},{}
    • 你可以拿走这个对象。你想用它做什么?
    • 从数组comments拉取对象并推送到另一个数组
    • 只需将对象推送到结果数组。 for (let { comments } of scores) result.push(...comments);.
    【解决方案2】:

    所有电子邮件都需要一个嵌套循环,但如果您知道要查找的电子邮件的索引,则只需将该索引传递给comments[index]

    scores[current_iteration].comments[current_iteration].email
    

    【讨论】:

      猜你喜欢
      • 2015-07-29
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 2014-05-14
      • 1970-01-01
      相关资源
      最近更新 更多