javascript - 如何通过键将对象数组映射到值数组

标签 javascript json

我想通过json结构中的关键字获取消息。例如json结构为:

[{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}

现在我想获取所有消息并将它们放入一个数组中。如何在javascript中实现这个?谢谢

最佳答案

var json = [{ _id: 123, message: 'hello', username: '1' }, { _id: 456, message: 'world', username: '2'}];
var arr = [];
for(i=0;i<json.length;i++) {
	arr.push(json[i].message);
}
console.log(arr);

关于javascript - 如何通过键将对象数组映射到值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40012997/

相关文章:

javascript - Android - 使用 webview 加载 URL 后,我可以更改背景颜色吗

html - 有没有办法阻止使用类似 Firebug 的工具编辑页面中的 HTML 和 CSS 内容?

javascript - 来自json数据的AngularJS动态表单(不同类型)

javascript - 使用 JavaScript 将项目添加到 Model.List<item>

python - 使用 json 序列化时跳过未知项目

javascript - 将 URL 中的数据添加到传单 map

javascript - 为什么在 webpack 中需要 html 文件时会得到位置字符串?

javascript - 选择网页上的超链接并使用 CasperJS 将生成的网页内容存储在文本文件中

json - 剑道 : Uncaught ReferenceError: uid is not defined

javascript - 分配 ng-model 时使用变量