从对象返回未定义的值

我的UD

因此,我正在努力获取名为的数据otherPartyName这些数据是从WebAPI提取的,并存储在称为的本地状态中dataSource我正在使用本机反应,想检查最新的createdBy是否等于otherPartyName。我可以获取的名称createdBy,但是由于不断获取,所以无法获取otherPartyName的值undefined

数据源

Array [
  Object {
    "comment": "Min vintage",
    "feedbackId": 32,
    "otherPartyName": "DooDooDooDooDoo",
    "replies": Array [
      Object {
        "comments": "Bruh ",
        "createdBy": "Min Cunt",
        "feedbackLogId": 32,
        "feedbackReplyLogId": 4,
      },
      Object {
        "comments": "So this is 1",
        "createdBy": "Min Cunt",
        "feedbackLogId": 32,
        "feedbackReplyLogId": 5,
      },
    ],
    "sender": "43434343",
    "type": "Improvement",
  },
]

这就是我如何获得最新的价值createdBy

let totalCount = dataSource.reduce((a, c) => a + c.replies.length, 0);
let reply = dataSource.map(({ replies }) => replies[totalCount - 1].createdBy)

但是无论我做什么,我都无法为otherPartyName定义

let otherParty = dataSource.otherPartyName
let otherParty = dataSource[0].otherPartyName
let otherParty = dataSource.map(({ otherParty }) => otherParty.otherPartyName)

如果我这样做console.log(dataSource),我可以得到上面显示的dataSource作为结果,但是如果我这样做,为什么我会变得未定义console.log(dataSource.otherPartyName)

Yosvel Quintero Arguelles

您可以otherPartyName使用Array.prototype.map()获得一个数组

const dataSource = [{ "comment": "Min vintage", "feedbackId": 32, "otherPartyName": "DooDooDooDooDoo", "replies": [{ "comments": "Bruh ", "createdBy": "Min Cunt", "feedbackLogId": 32, "feedbackReplyLogId": 4, }, { "comments": "So this is 1", "createdBy": "Min Cunt", "feedbackLogId": 32, "feedbackReplyLogId": 5, }], "sender": "43434343", "type": "Improvement", }]
const otherPartyNames = dataSource.map(ds => ds.otherPartyName)

console.log(otherPartyNames)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章