如何创建单个对象并使用javascript将它们列在数组中?

时间:2015-06-29 21:35:24

标签: javascript

我当前的数组并没有按照我的意愿创建单个对象。我的数据采用以下格式:

NSExtensionActivationSupportsImageWithMaxCount

我希望我的数据采用以下格式:

["1434100866", "599.0", "1434100870", "349.0", "1434100863", "233.0", "1434100849", "269.0", "1434100857", "682.0", "1434100862", "248.0", "1434100865", "342.0", "1434100848", "960.0", "1434100853", "270.0", "1434100850", "253.0"]

这是我的代码:

[[item1, item2],[item1, item2],[item1, item2], ...];

2 个答案:

答案 0 :(得分:2)

您希望push新数组,而不是新值。这样做:

var dataArray = new Array();
@foreach (var record in Model)
{
    @:dataArray.push(["@record.date", "@record.rate"]);
}        
console.log(dataArray);

答案 1 :(得分:0)

var a=["1434100866", "599.0", "1434100870", "349.0", "1434100863", "233.0", "1434100849", "269.0", "1434100857", "682.0", "1434100862", "248.0", "1434100865", "342.0", "1434100848", "960.0", "1434100853", "270.0", "1434100850", "253.0"];
var b = a.reduce(function (res, el, i) {
    if (i % 2) {
        res[res.length - 1].push(el);
    } else {
        res.push([el]);
    }
    return res;
}, []);
 document.getElementById('out').innerHTML = JSON.stringify(b, null, 4);
<pre id="out"></pre>