javascript - 如何在javascript中过滤json?
问题描述
好的,所以我的代码从 websocket 检索 json 并且当前将所有 json 打印到控制台。我想要做的是将信息片段存储为变量。我该如何转:
{
"type": "question",
"ts": "2018-06-30T00:05:53.685Z",
"totalTimeMs": 10000,
"timeLeftMs": 10000,
"questionId": 46220,
"question": "In wrestling, what term refers to pretending that scripted theatrics are totally real?",
"category": "Entertainment",
"answers": [{
"answerId": 140757,
"text": "Gas"
},
{
"answerId": 140758,
"text": "Kayfabe"
},
{
"answerId": 140759,
"text": "House show"
}
]
进入:
Question = In wrestling, what term refers to pretending that scripted theatrics are totally real?
pAnswer1 = Gas
pAnswer2 = Kayfabe
pAnswer3 = House show
我正在使用节点。
解决方案
这提供了你想要的。单击下面的“运行代码片段”以查看输出。
const obj = {
"type": "question",
"ts": "2018-06-30T00:05:53.685Z",
"totalTimeMs": 10000,
"timeLeftMs": 10000,
"questionId": 46220,
"question": "In wrestling, what term refers to pretending that scripted theatrics are totally real?",
"category": "Entertainment",
"answers": [{
"answerId": 140757,
"text": "Gas"
}, {
"answerId": 140758,
"text": "Kayfabe"
}, {
"answerId": 140759,
"text": "House show"
}]
}
const question = obj.question;
const pAnswer = obj.answers.map(ans => ans.text);
console.log('question =', question);
pAnswer.forEach((ans, i) => {
console.log(`answer${i+1} =`, ans);
})
推荐阅读
- laravel - Laravel JSON 分页:如何从下一个/上一个链接中删除箭头?
- html - 限制 svg 路径中的点数
- apache-kafka - 带有服务器发送事件 (SSE) 的 kafka 主题
- node.js - 谷歌语音算法。测试 Beta 功能
- mapbox - Mapbox 点不如 Mapbox studio 数据集中的点准确
- python - 根据列中唯一值的数量计算的新变量
- docker - 如何在普罗米修斯中公开 nginx 指标?
- components - Laravel Livewire 线:单击()删除组件属性?
- android - Android Studio:当我开始一个项目时,我分叉但得到一个错误“没有找到'app'的变体。”
- android - 如何在android中实现Realm DB上的Inner Join