gpt4 book ai didi

javascript - D3.js JSON forEach 映射到新数组

转载 作者:行者123 更新时间:2023-11-30 16:25:02 26 4
gpt4 key购买 nike

我有这个 JSON 文件,我想在我当前的 D3.js 项目中使用它。您可以从文件摘录中看到它包含几个信息。我从 this site 中抓取的(如果你对上下文感兴趣的话)

  [{
"rowid":1,
"Name":"Hasan Ali Saad al-Hareeri ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Daraa",
"Area":"Sawara",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":2,
"Name":"Riham Adnan Swais ",
"Status":"Civilian",
"Sex":"Adult - Female",
"Province":"Homs",
"Area":"Telbeiseh",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shelling"
},
{
"rowid":3,
"Name":"Ammar Ahmad al-Njoam ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Aleppo",
"Area":"Jarablus",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Explosion"
},
{
"rowid":4,
"Name":"Yaseen Slaiman al-Salkhadi ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Daraa",
"Area":"Jassim",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":5,
"Name":"Ayman Noman Qanatra ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Daraa",
"Area":"Jassim",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":6,
"Name":"Abdul Kareem Mohammad al-Tamki ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Daraa",
"Area":"Sheikh Miskeen",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":7,
"Name":"Ahmad Mohammad al-Tamki ",
"Status":"Civilian",
"Sex":"Child - Male",
"Province":"Daraa",
"Area":"Sheikh Miskeen",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":8,
"Name":"Hamze Mohammad al-Tamki ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Daraa",
"Area":"Sheikh Miskeen",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":9,
"Name":"Mohammad Ahmad al-Khaleel ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Daraa",
"Area":"Sheikh Miskeen",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shelling"
},
{
"rowid":10,
"Name":"Abdul Kareem Abdul Lateef al-Mohammad ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Hama",
"Area":"Lattamna",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":11,
"Name":"Mahdi Basem Qatteni ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Idlib",
"Area":"Khan Sheikhoun",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":12,
"Name":"Ahmad Ibraheem al-Sayed Omar ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Aleppo",
"Area":"",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":13,
"Name":"Unidentified ",
"Status":"Civilian",
"Sex":"Child - Male",
"Province":"Aleppo",
"Area":"Kafrnaya",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":14,
"Name":"Unidentified 1 ",
"Status":"Civilian",
"Sex":"Adult - Female",
"Province":"Aleppo",
"Area":"Bazzaa",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":15,
"Name":"Unidentified 2 ",
"Status":"Civilian",
"Sex":"Child - Male",
"Province":"Aleppo",
"Area":"Bazzaa",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":16,
"Name":"Unidentified 3 ",
"Status":"Civilian",
"Sex":"Child - Male",
"Province":"Aleppo",
"Area":"Bazzaa",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":17,
"Name":"Mohammad Ahmad al-Farhoud ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Idlib",
"Area":"Telminis",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":18,
"Name":"Yousef al-Saleh ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Idlib",
"Area":"Kafroumeh",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":19,
"Name":"Basel Hsain al-Knoah ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Idlib",
"Area":"Kafroumeh",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":20,
"Name":"Basel Abdul Rahman Qintareh ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Idlib",
"Area":"Kafroumeh",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":21,
"Name":"Hani Khairallah Jbaneej ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Idlib",
"Area":"Kafroumeh",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":22,
"Name":"Ahmad Mohammad al-Shartah ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Idlib",
"Area":"Kafroumeh",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":23,
"Name":"wife of Faisal al-Ahmad al-Khaleefe ",
"Status":"Civilian",
"Sex":"Adult - Female",
"Province":"Deir Ezzor",
"Area":"Qourieh",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":24,
"Name":"Unidentified ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Deir Ezzor",
"Area":"Takaya",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Kidnapping - Execution"
},
{
"rowid":25,
"Name":"Unidentified 1 ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Deir Ezzor",
"Area":"Tabya Jazera village",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Kidnapping - Execution"
},
{
"rowid":26,
"Name":"Unidentified 2 ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Deir Ezzor",
"Area":"Tabya Jazera village",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Kidnapping - Execution"
},
{
"rowid":27,
"Name":"Unidentified 3 ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Deir Ezzor",
"Area":"Tabya Jazera village",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Kidnapping - Execution"
},
{
"rowid":28,
"Name":"Unidentified 4 ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Deir Ezzor",
"Area":"Tabya Jazera village",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Kidnapping - Execution"
},
{
"rowid":29,
"Name":"Unidentified 5 ",
"Status":"Civilian",
"Sex":"Adult - Male",
"Province":"Deir Ezzor",
"Area":"Tabya Jazera village",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Kidnapping - Execution"
},
{
"rowid":30,
"Name":"Fayez Qabaqji ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Lattakia",
"Area":"Turkmen mount",
"Date_of_Death":"2015-12-10",
"Cause_of_Death":"Shooting"
},
{
"rowid":31,
"Name":"Fahed al-Asali ",
"Status":"Non-Civilian",
"Sex":"Adult - Male",
"Province":"Damascus",
"Area":"Jobar",
"Date_of_Death":"2015-12-09",
"Cause_of_Death":"Shooting"
},
{
"rowid":32,
"Name":"Amoun Murad ",
"Status":"Civilian",
"Sex":"Adult - Female",
"Province":"Idlib",
"Area":"Jisr Shagour: Mraand",
"Date_of_Death":"2015-12-09",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":33,
"Name":"Kafa Mezweq ",
"Status":"Civilian",
"Sex":"Adult - Female",
"Province":"Idlib",
"Area":"Jisr Shagour: Mraand",
"Date_of_Death":"2015-12-09",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":34,
"Name":"Hala Mohammad ",
"Status":"Civilian",
"Sex":"Adult - Female",
"Province":"Idlib",
"Area":"Jisr Shagour: Mraand",
"Date_of_Death":"2015-12-09",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":35,
"Name":"Ahmad Mezweq ",
"Status":"Civilian",
"Sex":"Child - Male",
"Province":"Idlib",
"Area":"Jisr Shagour: Mraand",
"Date_of_Death":"2015-12-09",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":36,
"Name":"Ahlam Mezweq ",
"Status":"Civilian",
"Sex":"Child - Female",
"Province":"Idlib",
"Area":"Jisr Shagour: Mraand",
"Date_of_Death":"2015-12-09",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":37,
"Name":"Adnan Hussain Haj Kheder ",
"Status":"Civilian",
"Sex":"Child - Male",
"Province":"Idlib",
"Area":"Jisr Shagour: Mraand",
"Date_of_Death":"2015-12-09",
"Cause_of_Death":"Warplane shelling"
},
{
"rowid":38,
"Name":"Wife of Hussain Haj Kheder ",
"Status":"Civilian",
"Sex":"Adult - Female",
"Province":"Idlib",
"Area":"Jisr Shagour: Mraand",
"Date_of_Death":"2015-12-09",
"Cause_of_Death":"Warplane shelling"
}]

我现在嵌套它,以便我可以按日期“Date_of_Death”、“Cause_of_Death”和“Province”对其进行排序,这样我就可以得到每个事件的伤亡总数,因为现在我有每个人的信息。我认为这应该对我有用。现在我遇到了无法获取每个省每天的事件总数的问题。我试图让它超过数组长度,但这没有成功。我错过了一些东西,但我不能确切地说出是什么。你能帮我把它映射到一个包含的新数组中吗有关日期、死因、死亡人数和发生的省份的信息?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Learning D3</title>
<link rel="stylesheet" href="main.css">
<script type="text/javascript" src="d3.min.js"></script>
</head>
<body>
<!--Place all DOM elements here -->
<script>

var martyr;

d3.json("martyrs_2015.json", function(error, json) {
if (error) return console.warn(error);
//martyr = json;
//console.log(martyr);
martyr = getGroupedDate(json);
console.log(martyr);
martyrrender(martyr);
});


function getGroupedDate(data){
var groupedDate = d3.nest()
.key(function(d) { return d.Date_of_Death; })
.key(function(d) { return d.Cause_of_Death; })
.key(function(d) { return d.Province })
.key(function(d) { return d.Area })
.entries(data);
return groupedDate;
}


function martyrrender(data){
console.log(data.length);
martyr.forEach(function (martyr) {
var arr = data.map(function(d){
return{
key: martyr,
value: d[martyr].length,
cause: d.Cause_of_Death,
}
});
console.log(arr);
});

}


</script>
</body>
</html>

最佳答案

您的要求是

现在我无法获得每个省份每天的事件总数,这让我陷入了困境

要根据省份获取第一组数据,请使用:

function getGroupedDate(data) {
//first group the data on the basis of province
var groupedDate = d3.nest()
.key(function(d) {
return d.Province;
})
.entries(data);
return groupedDate;
}

接下来使用此分组数据并将其分组为这样的死亡日期

function martyrrender(martyr) {
var arr = [];
martyr.forEach(function(martyr) {
//grouping the data on basis of Date_of_death
var groupedDate = d3.nest()
.key(function(d) {
return d.Date_of_Death;
})
.entries(martyr.values);

groupedDate.forEach(function(gd) {
//creating the array
var dta = {
province: martyr.key,//province data
date: gd.key,//date of death
value: gd.values.length//no of deaths happened in the provnce
}
arr.push(dta);
});
});
return arr;
}

上述函数将给出每个省在日期发生的死亡数组。

工作代码 here

在您提供的代码片段中,不能有多个用于分组的键,您可以只有一个键进行分组。

function getGroupedDate(data){
var groupedDate = d3.nest()
.key(function(d) { return d.Date_of_Death; })
.key(function(d) { return d.Cause_of_Death; })
.key(function(d) { return d.Province })
.key(function(d) { return d.Area })
.entries(data);
return groupedDate;
}

希望这对您有所帮助!

关于javascript - D3.js JSON forEach 映射到新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34240167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com