gpt4 book ai didi

javascript - 获取对象内数组内对象的属性

转载 作者:行者123 更新时间:2023-12-02 23:32:19 25 4
gpt4 key购买 nike

我有一个对象,当我使用 console.log 打印它而不进行 stringify 时,它看起来像这样:

SuiteStats {
type: 'suite',
start: 2019 - 06 - 04T13: 04: 10.640Z,
_duration: 6262,
uid: 'District1',
cid: '0-0',
title: 'District',
fullTitle: undefined,
tests: [],
hooks: [],
suites:
[SuiteStats {
type: 'suite',
start: 2019 - 06 - 04T13: 04: 15.271Z,
_duration: 1621,
uid: 'Create District5',
cid: '0-0',
title: '@sanity, @sanityUpgraded, @debug: Create District',
fullTitle: undefined,
tests: [Array],
hooks: [],
suites: [],
end: 2019 - 06 - 04T13: 04: 16.892Z
}
],
end: 2019 - 06 - 04T13: 04: 16.902Z

}

现在我想获取 SuiteStats 内套件内的 SuiteStats 标题(其值为:'@sanity、@sanityUpgraded、@debug:创建区')

console.log(SuiteStats.suites[0].title

不起作用,它退出并显示有关“TypeError:无法读取未定义的属性“标题””的错误

我尝试了其他方法但没有成功,例如:

console.log(SuiteStats.suites[0].SuiteStats.title

那么我做错了什么,为什么这个对象与我过去使用过的另一个对象不同?

这是对象在字符串化后的样子:

{
"type": "suite",
"start": "2019-06-04T13:29:25.385Z",
"_duration": 5575,
"uid": "District1",
"cid": "0-0",
"title": "District",
"tests": [],
"hooks": [],
"suites": [{
"type": "suite",
"start": "2019-06-04T13:29:29.737Z",
"_duration": 1220,
"uid": "Create District5",
"cid": "0-0",
"title": "@sanity, @sanityUpgraded, @debug: Create District",
"tests": [{
"type": "test",
"start": "2019-06-04T13:29:29.745Z",
"_duration": 369,
"uid": "I am logged in as admin user \"ufedadmin\"7",
"cid": "0-0",
"title": "I am logged in as admin user \"ufedadmin\"",
"output": [],
"state": "passed",
"end": "2019-06-04T13:29:30.114Z"
}, {
"type": "test",
"start": "2019-06-04T13:29:30.122Z",
"_duration": 523,
"uid": "I create or overwrite District \"Argentina3\" with code \"Argentina3\"8",
"cid": "0-0",
"title": "I create or overwrite District \"Argentina3\" with code \"Argentina3\"",
"output": [],
"state": "passed",
"end": "2019-06-04T13:29:30.645Z"
}, {
"type": "test",
"start": "2019-06-04T13:29:30.667Z",
"_duration": 283,
"uid": "I expect that district \"Argentina3\" was created9",
"cid": "0-0",
"title": "I expect that district \"Argentina3\" was created",
"output": [],
"state": "passed",
"end": "2019-06-04T13:29:30.950Z"
}
],
"hooks": [],
"suites": [],
"end": "2019-06-04T13:29:30.957Z"
}
],
"end": "2019-06-04T13:29:30.960Z"

}

最佳答案

看起来工作正常:

var data = {
"type": "suite",
"start": "2019-06-04T13:29:25.385Z",
"_duration": 5575,
"uid": "District1",
"cid": "0-0",
"title": "District",
"tests": [],
"hooks": [],
"suites": [{
"type": "suite",
"start": "2019-06-04T13:29:29.737Z",
"_duration": 1220,
"uid": "Create District5",
"cid": "0-0",
"title": "@sanity, @sanityUpgraded, @debug: Create District",
"tests": [{
"type": "test",
"start": "2019-06-04T13:29:29.745Z",
"_duration": 369,
"uid": "I am logged in as admin user \"ufedadmin\"7",
"cid": "0-0",
"title": "I am logged in as admin user \"ufedadmin\"",
"output": [],
"state": "passed",
"end": "2019-06-04T13:29:30.114Z"
}, {
"type": "test",
"start": "2019-06-04T13:29:30.122Z",
"_duration": 523,
"uid": "I create or overwrite District \"Argentina3\" with code \"Argentina3\"8",
"cid": "0-0",
"title": "I create or overwrite District \"Argentina3\" with code \"Argentina3\"",
"output": [],
"state": "passed",
"end": "2019-06-04T13:29:30.645Z"
}, {
"type": "test",
"start": "2019-06-04T13:29:30.667Z",
"_duration": 283,
"uid": "I expect that district \"Argentina3\" was created9",
"cid": "0-0",
"title": "I expect that district \"Argentina3\" was created",
"output": [],
"state": "passed",
"end": "2019-06-04T13:29:30.950Z"
}
],
"hooks": [],
"suites": [],
"end": "2019-06-04T13:29:30.957Z"
}
],
"end": "2019-06-04T13:29:30.960Z"
};

console.log(data.suites[0].title);

关于javascript - 获取对象内数组内对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56444730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com