var props = [
"current_day", "open_val", "high_val", "low_val", "close_val"
];
var data = [
{
"current_day":"2011-08-01",
"open_val":136.65,
"high_val":136.96,
"low_val":134.15,
"close_val":134.15
},
{
"current_day":"2011-08-02",
"open_val":135.26,
"high_val":135.95,
"low_val":131.50,
"close_val":131.85
}
]
var arrays = [];
for ( var i = 0; i < data.length; ++i )
{
var array = [];
for ( var j = 0; j < props.length; ++j )
{
array.push( data[i][props[j]] );
}
arrays.push(array);
}
console.log(JSON.stringify(arrays));
当我运行输出时
[["2011-08-01",136.65,136.96,134.15,134.15],["2011-08-02",135.26,135.95,131.5,131.85]]
您能告诉我如何在数组末尾添加close_value以便它看起来像这样
[["2011-08-01",136.65,136.96,134.15,134.15,134.15],["2011-08-02",135.26,135.95,131.5,131.85,131.85]]
答案 0 :(得分:1)
就像这样?更新了您的小提琴JSFiddle
var nprops = [
"close_val"
];
for ( var i = 0; i < data.length; ++i )
{
for ( var ii = 0; ii < arrays.length; ++ii )
{
for ( var j = 0; j < nprops.length; ++j )
{
var closeval = '';
closeval = data[i][nprops[j]] ;
}
arrays[ii].push(closeval);
}
}
for ( var i1 = 0; i1 < arrays.length; ++ii )
{
arrays[i1].pop();
}
console.log(JSON.stringify(arrays));
答案 1 :(得分:0)
您可以在内部for循环中使用if/else
:
var props = [
"current_day", "open_val", "high_val", "low_val", "close_val"
];
var data = [{
"current_day": "2011-08-01",
"open_val": 136.65,
"high_val": 136.96,
"low_val": 134.15,
"close_val": 134.15
}, {
"current_day": "2011-08-02",
"open_val": 135.26,
"high_val": 135.95,
"low_val": 131.50,
"close_val": 131.85
}]
var arrays = [];
for (var i = 0; i < data.length; ++i) {
var array = [];
for (var j = 0; j < props.length; ++j) {
if (props[j] !== "close_val") {
array.push(data[i][props[j]]);
} else {
array.push(data[i][props[j]]);
array.push(data[i][props[j]]);
}
}
arrays.push(array);
}
document.querySelector('pre').innerHTML = JSON.stringify(arrays);
&#13;
<pre></pre>
&#13;
答案 2 :(得分:0)
试试此代码
var props = [
"current_day", "open_val", "high_val", "low_val", "close_val"
];
var data = [{
"current_day": "2011-08-01",
"open_val": 136.65,
"high_val": 136.96,
"low_val": 134.15,
"close_val": 134.15
}, {
"current_day": "2011-08-02",
"open_val": 135.26,
"high_val": 135.95,
"low_val": 131.50,
"close_val": 131.85
}];
var arrays = [];
for (var i = 0; i < data.length; ++i) {
var array = [];
for (var j = 0; j < props.length; ++j) {
array[j] = data[i][props[j]];
}
arrays.push(array);
}
console.log(JSON.stringify(arrays));
&#13;