我敢肯定,下面是一个非常初学者的问题,很抱歉提出这个问题,但我已经很好地解决了这个问题,但没有运气……我希望“打破”或“扩展”以下内容:
var words = { hello: 2, there: 3, heres: 1, text: 1 }
进入这个:
var words = [{
word: 'hello',
count: 2
}, {
word: 'there',
count: 3
}, {
word: 'heres',
count: 1
}, {
word: 'text',
count: 1
}]
我一直在使用 Underscore.js,但肯定遗漏了一些非常明显的东西。任何帮助将不胜感激,谢谢!
原文由 Matt 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用
Object.keys()
和map()
执行此操作。您也可以先创建数组,然后使用
for...in
循环推送对象。