我有一个数组
var keys = ['Name','Id'];
我想与下面的字典对象合并
var projects = {
"project1": "11111",
"project2": "22222",
"project3": "33333",
};
生成
以下的输出output =
[
{ Name:"project1", Id:"11111"},
{ Name:"project2", Id:"22222"},
{ Name:"project3", Id:"33333"},
]
我尝试过使用
console.log(_.zipObject(keys, projects));
但是这很糟糕
如何使用lodash执行此操作?
答案 0 :(得分:0)
由于您要求专门使用 https.get(`https://api.openweathermap.org/data/2.5/weather?zip=${zip},us&APPID=${config.key}`, res => {
res.on('data', data => {
body = data.toString();
});
res.on('end', () => {
const parseData = JSON.parse(body);
fs.writeFile('data.txt', parsedData)
console.log(parsed_data);
});
});
module.exports = wrappedAll;
,因此您可以使用 lodash
<强>样本强>
_map
&#13;
var projects = {
"project1": "11111",
"project2": "22222",
"project3": "33333",
};
var result = _.map(projects, function(value, prop) {
return { Name: prop, id: value };
});
console.log(result);
&#13;
答案 1 :(得分:0)
使用Object.keys()
和Array.map()
创建对象数组。您可以使用computed property names:
var keys = ['Name','Id'];
var projects = {
"project1": "11111",
"project2": "22222",
"project3": "33333",
};
var result = Object.keys(projects)
.map(function(k) {
return {
[keys[0]]: k,
[keys[1]]: projects[k]
};
});
console.log(result);
&#13;