[英]Counting specific object properties values, if they match
我想計算匹配對象屬性值的數量。 更具體地講,如果我的結果object.place_name相同,則將下一項繼續計算,將其全部累加,然后使用res.render將此結果傳遞到前端
當前對象
results = [ { id: 'country.8605848117814600',
type: 'Feature',
place_type: [ 'country'
],
relevance: 1,
properties: { short_code: 'gb', wikidata: 'Q145'
},
text: 'United Kingdom',
place_name: 'United Kingdom',
bbox: [
-8.718659,
49.802665,
1.867399,
60.945453
],
center: [
-2,
54
],
geometry: { type: 'Point', coordinates: [Array
]
}
},
{ id: 'country.12399313490269000',
type: 'Feature',
place_type: [ 'country'
],
relevance: 1,
properties: { short_code: 'dk', wikidata: 'Q35'
},
text: 'Denmark',
place_name: 'Denmark',
bbox: [
7.970276,
54.3991486,
15.253716,
57.9322004
],
center: [
10,
56
],
geometry: { type: 'Point', coordinates: [Array
]
}
},
{ id: 'country.12399313490269000',
type: 'Feature',
place_type: [ 'country'
],
relevance: 1,
properties: { short_code: 'dk', wikidata: 'Q35'
},
text: 'Denmark',
place_name: 'Denmark',
bbox: [
7.970276,
54.3991486,
15.253716,
57.9322004
],
center: [
10,
56
],
geometry: { type: 'Point', coordinates: [Array
]
}
},
{ id: 'country.8605848117814600',
type: 'Feature',
place_type: [ 'country'
],
relevance: 1,
properties: { short_code: 'gb', wikidata: 'Q145'
},
text: 'United Kingdom',
place_name: 'United Kingdom',
bbox: [
-8.718659,
49.802665,
1.867399,
60.945453
],
center: [
-2,
54
],
geometry: { type: 'Point', coordinates: [Array
]
}
}
]
我正在嘗試運行的代碼
const promises = results.map(result =>
Promise.all([
geoPromise(result.Country_Name),
geoPromise(result.Organisation_Name),
result.Output_Title_Name
])
);
Promise.all(promises)
.then((values) => {
let results = values.map(elmt => elmt[0]);
console.log(results)
let businesses = values.map(elmt => elmt[1]);
let names = values.map(elmt => elmt[2]);
place_name = ['Denmark', 'United Kingdom']
count = results.reduce((s, o) => s + (o.place_name === place_name[0]), 0);
countObject = {place_name:'',
count:''}
countObject.place_name = place_name[0]
countObject.count = count
console.log(countObject)
res.render('layouts/layout', {
results: JSON.stringify(results),
businesses: JSON.stringify(businesses),
names: JSON.stringify(names),
resultCount: resultCount
});
})
但是,我的resultCount僅返回一個空數組。 有人可以描述我想要做的更好的方法嗎?
您可以使用Array#reduce
並計算條件是否使用來自對象的值和給定值得出的true
值。
var results = [{ id: 'country.8605848117814600', type: 'Feature', place_type: ['country'], relevance: 1, properties: { short_code: 'gb', wikidata: 'Q145' }, text: 'United Kingdom', place_name: 'United Kingdom', bbox: [-8.718659, 49.802665, 1.867399, 60.945453], center: [-2, 54], geometry: { type: 'Point', coordinates: [Array] } }, { id: 'country.12399313490269000', type: 'Feature', place_type: ['country'], relevance: 1, properties: { short_code: 'dk', wikidata: 'Q35' }, text: 'Denmark', place_name: 'Denmark', bbox: [7.970276, 54.3991486, 15.253716, 57.9322004], center: [10, 56], geometry: { type: 'Point', coordinates: [Array] } }, { id: 'country.12399313490269000', type: 'Feature', place_type: ['country'], relevance: 1, properties: { short_code: 'dk', wikidata: 'Q35' }, text: 'Denmark', place_name: 'Denmark', bbox: [7.970276, 54.3991486, 15.253716, 57.9322004], center: [10, 56], geometry: { type: 'Point', coordinates: [Array] } }, { id: 'country.8605848117814600', type: 'Feature', place_type: ['country'], relevance: 1, properties: { short_code: 'gb', wikidata: 'Q145' }, text: 'United Kingdom', place_name: 'United Kingdom', bbox: [-8.718659, 49.802665, 1.867399, 60.945453], center: [-2, 54], geometry: { type: 'Point', coordinates: [Array] } }]; place_name = 'Denmark', count = results.reduce((s, o) => s + (o.place_name === place_name), 0); console.log(count);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.