D3.js JSON forEach映射到新数组

时间:2015-12-12 13:07:49

标签: javascript arrays json d3.js

我有这个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”和“省”对它进行排序,这样我就可以得到每次事件的伤亡总数,因为现在我掌握了每个人的信息。我认为这对我有用。现在我遇到了一个问题,即我每天都无法获得每个省的事故总数。我试图在数组长度上得到它,但这没有成功。我错过了一些东西,但我无法确切地知道是什么。你能帮我把它映射成一个包含的新数组吗? 有关日期,死因,死亡人数和发生的省份的信息?

<!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>

1 个答案:

答案 0 :(得分:2)

您的要求是

现在我遇到的问题是我每天都无法获得每个省的事故总数

使用以下方式获取第一组数据:

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;
}

希望这有帮助!