我有这个json结构。
x=[{
"value": 1.37,
"date_transacted": "2015-01-01"
}]
从这个json结构中,我想生成以下json结构;
y1=[{
c: [{
v: "2015-01-01"
},
{
v: "1.37"
}]
}]
我已经编写了代码来执行此操作。它看起来像这样;
var y1 = [{ c:[ {"v":x[0].value}, {"v":x[0].date_transacted} ] }];
当x
有几个json键/值对时,我的问题出现了。看起来像这样的东西;
x=[{
"value": 1.37,
"date_transacted": "2015-01-01"
},
{
"value": 1.62,
"date_transacted": "2015-02-01"
},
{
"value": 1.83,
"date_transacted": "2015-03-01"
}]
通过对象数组迭代我的代码以生成所需的json结构的有效方法是什么?
y=[{
c: [{
v: "2015-01-01"
},
{
v: "1.37"
}]
},
{
c: [{
v: "2015-01-02"
},
{
v: "1.62"
}]
},
{
c: [{
v: "2015-01-03"
},
{
v: "1.83"
}]
}]
答案 0 :(得分:1)
作为单一陈述:
y = x.map(function(e)
{
return {
c:
[
{
v: e.value
},
{
v: e.date_transacted
}
]
};
});
答案 1 :(得分:1)
此处的其他答案(@ user2415266除外)不是动态的,硬编码接受精确输入,并且不是特别可重复使用。如果您有超过2个属性,或者在@ Siguza的情况下,如果您的属性不是'date_transacted'和'value',它们将会失败。
function restructureJson(obj) {
var output = {c:[]};
for (var i in obj) {
output.c.push({v:obj[i]});
}
return output;
}
此函数可在任何大小的任何对象数组中重复使用,包含任意数量的属性。
// Simple example
var json1 = [{
"value": 1.37,
"date_transacted": "2015-01-01"
}];
// More complex
var json2 = [{
"value": 1.37,
"date_transacted": "2015-01-01",
"another_value": "test",
"more": "12356"
},
{
"value": 1.62
},
{
"value": 1.83,
"date_transacted": "2015-03-01",
"stuff": "124334654567"
}];
// Map the function to the arrays
a = json1.map(restructureJson);
b = json2.map(restructureJson);
答案 2 :(得分:0)
initTestBlock('mouse', {
start: 'mousedown',
move: 'mousemove',
end: 'mouseup'
});
initTestBlock('touch', {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
});
initTestBlock('touch-no-remove', {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
}, true);
function initTestBlock(id, events, noRemove) {
var block = document.getElementById(id);
var parent = block.querySelector('.parent');
var target = block.querySelector('.target');
target.addEventListener(events.start, function(e) {
console.log(e.type);
if (!noRemove) {
setTimeout(function() {
// Remove target
target.parentElement.removeChild(target);
}, 1000);
}
function onMove(e) {
console.log(e.type);
var pt = getCoords(e);
parent.style.left = pt.x + 'px';
parent.style.top = pt.y + 'px';
}
function onEnd(e) {
console.log(e.type);
window.removeEventListener(events.move, onMove);
window.removeEventListener(events.end, onEnd);
}
window.addEventListener(events.move, onMove);
window.addEventListener(events.end, onEnd);
});
}
// Returns pointer coordinates
function getCoords(e) {
if (e instanceof TouchEvent) {
return {
x: e.touches[0].pageX,
y: e.touches[0].pageY
};
}
return {
x: e.pageX,
y: e.pageY
};
}
window.addEventListener('selectstart', function() {
return false;
}, true);
答案 3 :(得分:0)
其他答案仅在x中的对象具有确切的2个属性时才起作用。 无论属性数量多少,都可以这样做:
(gdb) r
Starting program: /Users/sal/Katiss/ecodriving
Unable to find Mach task port for process-id 39278: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))
修改:使用地图
y = [];
for(var i = 0, i < x.length; i++){
var obj = {c:[]};
for(var prop in x[i]){
obj.c.push({v:x[i][prop]})
}
y.push(obj);
}