我需要使用请求模块从website.json获取数据,然后需要对这些数据进行排序,并且仅以key:value格式存储该值和该值,例如“ thatvalue”:thisvalue。我需要对所有数据进行此操作。
示例数据
{"number": ewe, "amount": [{"added": 757335, "normal": {"size": 2, "s": "0.021", "ww": "0.37", "fe": "0.15", "gr": 3}, "that": "2354", "d": "0.1", "g": "dwe wew", "ff": "0.2", "this": 4, "dwa": {"ewa": 2, "dass": "0.236", "da": "2.1", "d": "0.6", "tr": 268}, "f2p": false,
当前代码
Helper.update = function(body){
this.debug("Updating");
request("website.json", {json: true},
console.log(body),
);
}
答案 0 :(得分:0)
如果我理解正确,这是使用lodash的快速示例:
let obj = _.map(data.amount, function(item){
let _this = item.this;
let _that = item.that;
return { [_this]: _that }
});
console.log(obj);
正在运行的演示(带有有效的任意json对象):doc
使用Array.prototype.map,无需外部库:
let obj = data.amount.map(function(item){
let _this = item.this;
let _that = item.that;
return { [_this]: _that }
});
console.log(obj);