javascript - 在 JavaScript 数组中显示对象
问题描述
我有一个数组,
var test = [{
id: 0,
test_id: "Password test",
pass: 1,
fail: 5,
time: 0.03,
pass_fail: 20,
comments : [
{comment : "a comment", commentuser : "user" },
]
};
]
在我想在对象comments中获取comment和commentuser的值。我尝试如下,
JSON.stingify(test.comments) // output : "{"comment":"a comment","commentuser":"user"}"
有没有办法获得价值?想要的输出:“评论,用户”
干杯
解决方案
加入:Object.values
_test[0].comments[0]
var test = [{
id: 0,
test_id: "Password test",
pass: 1,
fail: 5,
time: 0.03,
pass_fail: 20,
comments: [{
comment: "a comment",
commentuser: "user"
}]
}]
var result = Object.values(test[0].comments[0]).join(',');
console.log(result);
或深度解构您想要的对象并加入Object.values
:
var test = [{
id: 0,
test_id: "Password test",
pass: 1,
fail: 5,
time: 0.03,
pass_fail: 20,
comments: [{
comment: "a comment",
commentuser: "user"
}]
}]
var [{
comments: [obj]
}] = test;
var result = Object.values(obj).join(',');
console.log(result);
推荐阅读
- excel - 验证是否已经使用 VBA 创建了时间线
- sql - 如何创建唯一 ID 列以匹配现有列
- java - Spring Boot 和 JPA+Postges:Lob 映射到文本但整数被持久化
- linux - 是否有 data: Linux 上类似 URI 的路径构造?
- python - 从 k 个字母的字符串中构建一个函数,该函数从有效单词列表中输出单词列表,并找到拼字游戏得分最高的单词
- r - 自动执行脚本,以便使用批处理文件和/或 Windows 任务调度程序将对象保存到全局环境中
- amazon-web-services - 与 S3 同步:Errno 24 打开的文件过多
- python - 没有等待的异步任务队列
- java - 在 android studio 的选项卡布局中看不到小部件
- c# - 在带有 ClosedXML 的列验证中使用 INDIRECT