我有dataset1.grants,其中包含表单中的对象
您可以在下面注意到主要对象称为授权,其中包含一个名为值的重要字段,并且一个对象嵌套在授予内部"组织" ,其中包含重要的国家/地区字段。
我想计算按国家/地区列出的所有值的总和。还有500多个对象和5个以上的国家/地区。我想创建一个数组对象,其中包含{country:" england",Total_value:23455623}这样的内容:
如何继续比较我所拥有的CountryList数组和名为Organisation.Country的嵌套对象,并将++值添加到一个国家/地区的总数中。
代码:
var countries = {};
dataset1.organisations.forEach(function(org)
{
countries[org.Country]=true //gets all the countries from dataset and sets it to true in countries object
})
var countryList = []; //has list of countries in array
for (var key in countries)
{
countryList.push(key);
}
对象
{
"grant": 0,
"ID": "EP/E027261/1",
"Title": "Semiconductor Research at the Materials-Device Interface",
"PIID": "6674",
"Scheme": "Platform Grants",
"StartDate": "01/05/2007",
"EndDate": "31/10/2012",
"Value": "800579",
"ResearchArea": "Non CMOS Device Technology",
"Theme": "Information and Communication Technologies",
"Department": "Electrical and Electronic Engineering",
"OrgID": "93",
"Investigators": [
{
"ID": "6674",
"Role": "Principal Investigator"
},
{
"ID": "29195",
"Role": "Co Investigator"
},
{
"ID": "90639",
"Role": "Co Investigator"
},
{
"ID": "101342",
"Role": "Co Investigator"
},
{
"ID": "12223",
"Role": "Co Investigator"
},
{
"ID": "45348",
"Role": "Co Investigator"
},
{
"ID": "96538",
"Role": "Co Investigator"
},
{
"ID": "10965",
"Role": "Co Investigator"
}
],
"Collaborators": [],
"Summary": "This proposal concerns research into electronic materials, and the development of experimental methods designed to improve our measurement capability on the nm scale. Semiconductor materials and devices are central to manufacturing, healthcare, security, administration and leisure. This pivotal position in our lives has developed gradually but is due in the main to dramatic changes that have occurred quite recently. Over the last decade semiconductor technology has begun to experience a revolution in terms of functionality based on decreased size and increased complexity, and this trend will define the future for the entire manufacturing sector. This presents immense challenges to both researchers and to manufacturers of semiconductors because the key issues are no longer the properties of bulk materials or even two-dimensional structures but the properties of small heterogeneous clusters of atoms (semiconductor, dielectric and metal) that constitute today's functional device. To put this into context, the next generation silicon NMOS transistor (45nm node) is only half the size of an influenza virus and for most applications will work in conjunction with tens of millions of similar devices. For research, development and control in manufacture the electronic and physical properties of small atomic clusters need to be probed and interactions with structures in close proximity understood.As materials and device sub-structures become more complex the experimental task of obtaining precise information becomes ever more challenging. In particular the atomic organisation and local chemistry can have a profound effect on electronic behaviour and there is a growing need to develop measurement methods which can both image structures and link shape with local spectroscopic information. In our work we are pushing forward such methods by combining x-ray spectroscopy with scanning probe imaging, using both national and international synchrotron radiation sources. In a complementary approach, we are extending electron energy loss techniques in scanning transmission electron microscopy to link chemical and structural information. Optical spectroscopy is an invaluable tool for characterising condensed matter and we are developing free electron laser pumped Raman spectroscopy in order to directly probe electron states in ultra small semiconductors.Almost all emerging device technologies are limited by these materials issues and much of our work is guided by measuring and understanding these. For example, ultra high speed, low noise detectors and amplifiers are desperately needed by radio-astronomers for the next generation of telescopes. Such devices demand near perfect material and interface properties and form part of our programme. Similarly future THz emitters are hugely challenging in terms of materials physics. One of the key developments in electronic materials in the last decade is the ability to synthesise quantum dots which give three dimensional control over quantum size effects and hold the promise of highly tuneable materials. Measuring the collective electrical properties has proved a major task and the information required to build many devices is missing. We are extending and adapting point defect measurement methods to close this gap. The increasing complexity of materials raises many issues for the device and circuit designer. An important feature of our proposed work is that we aim to include device design concepts at the materials level, and will use this work to guide our experimental programme.",
"organisation": {
"organisation": 93,
"OrgID": "93",
"Name": "The University of Manchester",
"City": "Manchester",
"Region": "Greater Manchester",
"Country": "England",
"Postcode": "M13 9PL",
"Latitude": "53.4668498",
"Longitude": "-2.2338837"
}
}
答案 0 :(得分:3)
您可以使用此ES6脚本,该脚本使用Map
汇总按国家/地区键入的值,然后将该结果与Array.from
转换为您需要的对象数组:
var result = Array.from(
dataset1.grants.reduce( (countries, grant) =>
countries.set(grant.organisation.Country,
(countries.get(grant.organisation.Country) || 0) + +grant.Value),
new Map() ),
([country, sum]) => ({ country, sum })
);
简化示例数据的摘录:
var dataset1 = {
"grants": [{
"Value": "800579",
"organisation": {
"Country": "England"
}
},{
"Value": "100",
"organisation": {
"Country": "England"
}
},{
"Value": "200",
"organisation": {
"Country": "England"
}
},{
"Value": "1",
"organisation": {
"Country": "Belgium"
}
}]
};
var result = Array.from(
dataset1.grants.reduce( (countries, grant) =>
countries.set(grant.organisation.Country,
(countries.get(grant.organisation.Country) || 0) + +grant.Value),
new Map() ),
([country, sum]) => ({ country, sum })
);
console.log(result);
代码以new Map()
开头,创建Map object,可用于按键组织数据(很像普通对象具有键/值)。这个空的Map是在reduce
迭代授权时累积的初始值,并为每个授权调用(箭头)函数。
此箭头函数使用两个参数:累计值,即Map
对象(名为 countries )和当前授权对象(名为 grant )。 Map的get
方法用于从该地图中检索我们已经为该国家/地区提供的内容。如果找不到任何内容,|| 0
将启动,而将使用0代替。
对于该值,当前授权的值与+ +grant.Value
一起添加。第二个加号是一个将字符串转换为数字的单一加号,因为从您的示例中可以看出Value
属性具有字符串值。
此总和使用set
方法在授权所在国家/地区键入的条目中存储回地图。 set
方法的返回值是整个Map。这很好,因为reduce
回调必须返回累积值,这就是我们的Map。然后reduce
将再次为下一个grant对象调用回调,并作为第一个参数传递我们在前一个调用中返回的内容,因此累积的Map从一次迭代传递到下一次迭代。在最后调用时,此返回值将成为reduce
方法本身的返回值。
现已完成的地图包含您需要的信息,即每个国家/地区的总和。但是当你没有寻找一个Map,而是一个对象数组时,又执行了一个步骤:Array.from
可以将一个映射转换为一对数组(即具有2个元素的子数组)。这很接近,但还不是你要求的。
现在,Array.from
接受一个函数作为其第二个参数,它允许您将每个元素(对)映射(转换)为其他元素。因此,在这种情况下,我们将对[country, sum]
转换为对象{country, sum}
。这里我们使用几个ES6特性:一个是我们可以用解构语法定义函数参数,另一个是我们可以使用{country: country, sum: sum}
的快捷符号。
最后一个箭头函数中必须使用一些括号,以避免JavaScript误解括号和/或大括号。
答案 1 :(得分:1)
这与构建直方图非常相似。我建议对使用直方图的不同方式进行谷歌搜索,这是教育性的。
在你的情况下,这就是我完成任务的方式。
var countries = dataset1.reduce(function(countryList, currentGrant) {
var country = currentGrant.organisation.Country;
var value = currentGrant.Value;
if (!countryList[country]) countryList[country] = 0;
countryList[country] += value;
return countryList;
}, {});
现在您拥有{country1:value1 ...}格式的数据,您可以构建您想要的任何数据结构。
var countries = [];
for (country in countryList) {
countries.push({country: country, totalValue: countryList[country]});
}