Javascript 筛选特定属性并对值求和
我有一个多边形的传单地图,你可以点击每个多边形来选择它们,并且有一个信息窗口“L.control”,显示所选多边形的值。继续单击“多边形”时,“信息”窗口会为每个选定多边形添加值,并获得所有选定多边形的总值。所有这些都很好,但我需要对特定属性进行更详细的汇总,如下面的区域示例。如果选择了十个多边形,我想区分属性为“REGION SOUTH”和“REGION NORTH”的区域的总数以及所有多边形的总数。 这是我正在使用的代码,对不同属性的总和求和没有问题,但是如何对定义的属性求和 如何以及在何处添加一种只对所需属性求和的过滤解决方案Javascript 筛选特定属性并对值求和,javascript,filter,leaflet,Javascript,Filter,Leaflet,我有一个多边形的传单地图,你可以点击每个多边形来选择它们,并且有一个信息窗口“L.control”,显示所选多边形的值。继续单击“多边形”时,“信息”窗口会为每个选定多边形添加值,并获得所有选定多边形的总值。所有这些都很好,但我需要对特定属性进行更详细的汇总,如下面的区域示例。如果选择了十个多边形,我想区分属性为“REGION SOUTH”和“REGION NORTH”的区域的总数以及所有多边形的总数。 这是我正在使用的代码,对不同属性的总和求和没有问题,但是如何对定义的属性求和 如何以及在何处
$.each(statesData.features, function(index, feature) {
var name = `${feature.properties.ZIPCODE} ${feature.properties.Name} ( ${feature.properties.average_time} - ${feature.properties.CITY})`
placenames.push(name);
zipcodes[name] = feature.properties.ZIPCODE;
time = feature.properties.average_time
});
etc....
// Now get the totals of selected polygons
var detailshow = function() {
var result = ''
var total = 0
var total1 = 0
var total2 = 0
var total3 = 0
var total4 = 0
for (var i = 0; i < featuresSelected.length; i++) {
var properties = featuresSelected[i].feature.properties
result +=
`
${properties.CITY}<br>
Zipcode: ${properties.ZIPCODE}
<a href="#" onclick=dellayer(${properties.ZIPCODE})>Delete</a>
<hr>`;
total += properties.amount, // sum amount for all regions
total1 += properties.average_time, // in seconds
total2 += properties.distance,
total3 += properties.amount, // amount for Region South only
total4 += properties.amount, // amount for Region North only
// Convert seconds to timeformat
var convertTime = function (input, separator) {
var pad = function(input) {return input < 10 ? "0" + input : input;};
return [
pad(Math.floor(input / 3600)),
pad(Math.floor(input % 3600 / 60)),
pad(Math.floor(input % 60)),
].join(typeof separator !== 'undefined' ? separator : ':' );
}
var resultTime = convertTime(total1);
}
return {
result: result,
total: total,
resultTime: resultTime,
total2: total2
total3: total3
total4: total4
};
}
detailsselected.update = function(arrayselected) {
var details = detailshow()
this._div.innerHTML =
'<b>Zipcodes</b><br>' +
'Total time: <b>' + details.resultTime + ' hh:mm:ss</b><br>' +
'Total amount: <b>' + details.total + ' st</b><br>' +
'Region South amount: <b>' + details.total3 + ' st</b><br>' +
'Region North amount: <b>' + details.total4 + ' st</b><br>' +
'Distance: <b>' + details.total2.toFixed(1) + ' km</b><br>';
$('#suma', window.parent.document).val(details.resultTime, details.total, details.total2, details.total3, details.total4);
};
detailsselected.addTo(map);
我确实尝试了以下方法,但没有成功
function filt_north (feature){
if (feature.properties.REGION === 'REGION NORTH' )
return true;
}
total4 += filt_north.(properties.amount), // amount for Region North only
您编写的
filt\u north
函数看起来不错,只需添加一个filt\u south
过滤器即可获得南部区域并执行以下操作:
let filteredResults = featuresSelected.filter(
result => filt_north(result.feature) || filt_south(result.feature)
);
for (let result of filteredResults) {
var properties = result.feature.properties;
...
尝试了你的解决方案,似乎它破坏了代码,并且总数根本没有加起来=停止工作。我这样做了,是否应该用不同的方式? 过滤功能:
function filt_south (feature){
if (feature.properties.REGION === 'REGION SOUTH')
return true;
}
function filt_north (feature){
if (feature.properties.REGION === 'REGION NORTH')
return true;
}
然后换成这个(我一定是做错了什么):
//现在获取选定多边形的总数
var detailshow=函数(){
var结果=“”
var总计=0
var total1=0
var total2=0
var total3=0
var total4=0
让filteredResults=功能Selected.filter(
结果=>filt_south(result.feature)| filt_north(result.feature)
);
for(让筛选结果返回结果){
变量属性=result.feature.properties;
对于(var i=0;i
Zipcode:${properties.Zipcode}
`;
合计+=properties.amount,//所有区域的合计金额
total1+=属性。平均_时间,
total2+=属性.distance,
total3+=filt_south(properties.amount),//仅南部地区的金额
total4+=filt_north(properties.amount)//仅适用于北部地区的金额
//将秒转换为时间格式
var convertTime=函数(输入,分隔符){
var pad=函数(输入){返回输入<10?“0”+输入:输入;};
返回[
pad(数学楼层(输入/3600)),
pad(数学楼层(输入%3600/60)),
pad(数学楼层(输入%60)),
].join(分隔符的类型!=“未定义”?分隔符:“:”);
}
var resultTime=转换时间(总计1);
}
}
返回{
结果:结果,,
总计:总计,
结果时间:结果时间,
总计2:总计2
总计3:总计3
总计4:总计4
};
}
detailsselected.update=函数(arrayselected){
var details=detailshow()
这是.\u div.innerHTML=
“Zipcode
”+
'总时间:'+details.resultTime+'hh:mm:ss
'+
'总金额:'+details.Total+'st
'+
'地区南部金额:'+details.total3+'st
'+
'地区北部金额:'+details.total4+'st
'+
“距离:”+细节。总计2.固定距离(1)+“公里
”;
$('#suma',window.parent.document).val(details.resultTime,details.total,details.total2,details.total3,details.total4);
};
已选择详细信息。添加到(地图)
所选功能[0]看起来像什么?也添加了该部分,谢谢
let filteredResults = featuresSelected.filter(
result => filt_north(result.feature) || filt_south(result.feature)
);
for (let result of filteredResults) {
var properties = result.feature.properties;
...
function filt_south (feature){
if (feature.properties.REGION === 'REGION SOUTH')
return true;
}
function filt_north (feature){
if (feature.properties.REGION === 'REGION NORTH')
return true;
}
// Now get the totals of selected polygons
var detailshow = function() {
var result = ''
var total = 0
var total1 = 0
var total2 = 0
var total3 = 0
var total4 = 0
let filteredResults = featuresSelected.filter(
result => filt_south(result.feature) || filt_north(result.feature)
);
for (let result of filteredResults) {
var properties = result.feature.properties;
for (var i = 0; i < featuresSelected.length; i++) {
var properties = featuresSelected[i].feature.properties
result +=
`
${properties.CITY}<br>
Zipcode: ${properties.ZIPCODE}
<a href="#" onclick=dellayer(${properties.ZIPCODE})>Delete</a>
<hr>`;
total += properties.amount, // sum amount for all regions
total1 += properties.average_time,
total2 += properties.distance,
total3 += filt_south (properties.amount), // amount for Region South only
total4 += filt_north (properties.amount) // amount for Region North only
// Convert seconds to timeformat
var convertTime = function (input, separator) {
var pad = function(input) {return input < 10 ? "0" + input : input;};
return [
pad(Math.floor(input / 3600)),
pad(Math.floor(input % 3600 / 60)),
pad(Math.floor(input % 60)),
].join(typeof separator !== 'undefined' ? separator : ':' );
}
var resultTime = convertTime(total1);
}
}
return {
result: result,
total: total,
resultTime: resultTime,
total2: total2
total3: total3
total4: total4
};
}
detailsselected.update = function(arrayselected) {
var details = detailshow()
this._div.innerHTML =
'<b>Zipcodes</b><br>' +
'Total time: <b>' + details.resultTime + ' hh:mm:ss</b><br>' +
'Total amount: <b>' + details.total + ' st</b><br>' +
'Region South amount: <b>' + details.total3 + ' st</b><br>' +
'Region North amount: <b>' + details.total4 + ' st</b><br>' +
'Distance: <b>' + details.total2.toFixed(1) + ' km</b><br>';
$('#suma', window.parent.document).val(details.resultTime, details.total, details.total2, details.total3, details.total4);
};
detailsselected.addTo(map)