Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将对象拆分为两个属性_Javascript_Underscore.js - Fatal编程技术网

Javascript 将对象拆分为两个属性

Javascript 将对象拆分为两个属性,javascript,underscore.js,Javascript,Underscore.js,我相信下面是一个非常初级的问题,我很抱歉问这个问题,但我在这件事上做得很好,没有运气。。。我希望“打破”或“扩展”以下内容: 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'

我相信下面是一个非常初级的问题,我很抱歉问这个问题,但我在这件事上做得很好,没有运气。。。我希望“打破”或“扩展”以下内容:

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
}]

我一直在使用下划线.js,但肯定遗漏了一些非常明显的东西。任何帮助都将不胜感激,谢谢

您可以使用
Object.keys()
map()
执行此操作

var words={hello:2,there:3,heres:1,text:1}
var result=Object.keys(words).map(e=>({word:e,count:words[e]}))

console.log(结果)
使用
数组#映射的可能解决方案

const words={hello:2,there:3,heres:1,text:1},
res=Object.keys(words.map)(v=>({word:v,count:words[v]}));

控制台日志(res)下面是一个使用下划线函数的解决方案:

words = _.map(words, (v, k) => ({word: k, count: v}));

下划线的映射可以在对象上迭代。迭代对象的第一个参数是值,第二个参数是键。

Hey@Nenad我有问题,但它实际上与所问的问题相反,你能帮我解决吗。
let object = {
  "06.10 15:00": 3.035,
  "06.10 21:00": 3.001,
};

let arr = [];

for (const [key, value] of Object.entries(object)) {
  arr.push({ date: key, value: value });
}

console.log(arr);