0
我有JSON數據,並且已將該數據轉換爲對象數組,並將該數組分配給另一個使用.slice(0)方法的數組。array1.splice(1,1)正在從我使用array1.slice創建的array2中刪除數據(0)
當我試圖從指定的數組中刪除某個元素時,它將從原始元素中刪除相同的元素。我不想在任何時候改變我的原始數組。
var values = [
{
"timeId":"201501010100",
"Week1Volume": 2454721,
"Week2Volume": 7017731,
"Week3Volume": 5656528,
"Week4Volume": 2472223
},
{
"timeId":"201501010200",
"Week1Volume": 2454721,
"Week2Volume": 7017731,
"Week3Volume": 5656528,
"Week4Volume": 2472223
},
{
"timeId":"201501010300",
"Week1Volume": 1999120,
"Week2Volume": 5355235,
"Week3Volume": 5120254,
"Week4Volume": 2607672
}]; // This is my JSON data
var weekNames = d3.keys(values[0]).filter(function(key) { return key !== "timeId" && key!== "weeksVolume";});;
values.forEach(function(d) {
d.weeksVolume = weekNames.map(function(name) { return {name: name, value: +d[name]}; });
}); // original array
var data = values.slice(0); // Data is my new array
function removeFromSecondArray(){
// Here I am removing the first object of weekVolumes from each object of the array
data.forEach(function(d) {
d.weeksVolume.splice(1,1);
});
};
但執行上述功能後,「值」的第一個元素也被刪除。
看看這個答案:http://stackoverflow.com/questions/37006725/creating-series-from-series-in-d3/37007424#37007424它可以幫助你 – thatOneGuy