这似乎应该很简单,但我一直在研究这个问题,但仍然感到难过。 Here is a Codepen link to what I have so far.
我需要允许用户选择一个数据集(通过按钮),然后将该数据集添加到那时已经选择的任何其他数据集中,不会有超过3或4个数据集。我的代码现在看起来像这样:
HTML:
<button onclick="createDataSet(data1);">Add Data 1</button>
<button onclick="createDataSet(data2);">Add Data 2</button>
<button onclick="makeChart();">Make Chart</button>
JS:
var data1 = [{...},{...}];
var data2 = [{...},{...}];
// function is supposed to create a new concatenated dataset
function createDataSet(data) {
var dataSet = [];
dataSet = dataSet.push(data);
console.log(dataSet);
return dataSet;
};
function makeChart(chartData){
LoadChart(chartData);
}
// will make the following functional later
function LoadChart(){
console.log("chart is made");
}
我尝试过使用concat(),如下所示:
function createDataSet(data) {
var temp = [];
var dataSet = [];
temp = dataSet.concat(data);
console.log(temp);
return dataSet;
};
但这并没有起作用,我希望我的方法现在在某种程度上存在根本性的缺陷。 Again, here is a Codepen link to what I have so far.
感谢阅读!
答案 0 :(得分:1)
您可以使用Array.prototype.concat()将数组连接到数组变量$(document).ready(function(){
$('#myTable').tablesorter({
widgets: ['staticRow']
});
} );
<table id="myTable" class="tablesorter {sortlist: [[0,0],[4,0]]}">
:
dataSet
var data1 = [{"fake": 2132,"data": 2676,"example": "human"}, {"fake": 2132,"data": 2676,"example": "human"}, {"fake": 2132,"data": 2676,"example": "human"}, {"fake": 2132,"data": 2676,"example": "human"}, {"fake": 2132,"data": 2676,"example": "human"}, {"fake": 2132,"data": 2676,"example": "human"}, {"fake": 2132,"data": 2676,"example": "human"}],
data2 = [{"fake": 32,"data": 76,"example": "human"}, {"fake": 32,"data": 76,"example": "human"}, {"fake": 32,"data": 76,"example": "human"}, {"fake": 32,"data": 76,"example": "human"}, {"fake": 32,"data": 76,"example": "human"}, {"fake": 32,"data": 76,"example": "human"}, {"fake": 32,"data": 76,"example": "human"}],
dataSet = [];
function createDataSet(data) {
dataSet = dataSet.concat(data);
}
function makeChart(dataSet){
console.log(dataSet);
LoadChart(dataSet);
}
// will make the following functional later
function LoadChart(){
console.log("chart is made");
}