3 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以使用 javascript function filter(),并使用函數(shù)作為回調(diào)使用類型。
const array = [
{ Type: "Food", Price: "100" },
{ Type: "Entertainment", Price: "200" },
{ Type: "Food", Price: "80" },
{ Type: "Entertainment", Price: "150" }
];
let type = 'Food';
var found = array.find(findType(type));
function findType(type) {
return (item) => item.Type === type;
}
console.log(found);
// Object { Type: "Food", Price: "100" }

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
您將希望將數(shù)據(jù)減少為一個(gè)總和。如果它們的鍵等于“食物”,您只會(huì)添加到總和(運(yùn)行總數(shù))中。
您從零開(kāi)始,并添加解析的整數(shù)(因?yàn)槟魂P(guān)心小數(shù))價(jià)格每個(gè)具有“食物”鍵的項(xiàng)目。
編輯: reducer的邏輯如下:
TOTAL + (DOES_KEY_MATCH? YES=PARSED_VALUE / NO=ZERO);
const array = [
{ Type: "Food", Price: "100" },
{ Type: "Entertainment", Price: "200" },
{ Type: "Food", Price: "80" },
{ Type: "Entertainment", Price: "150" }
];
/**
* Returns the sum of targeted values that match the key.
* @param data {object[]} - An array of objects.
* @param key {string} - The value of the key that you want to match.
* @param options.keyField [key] {string} - The key field to match against.
* @param options.valueField [value] {string} - The value of the matching item.
* @return Returns the sum of all items' values that match the desired key.
*/
function calculateTotal(data, key, options) {
let opts = Object.assign({ keyField: 'key', valueField: 'value' }, options || {});
return data.reduce((sum, item) => {
return sum + (item[opts.keyField] === key ? parseInt(item[opts.valueField], 10) : 0);
}, 0);
}
console.log('Total cost of Food: $' + calculateTotal(array, 'Food', {
keyField: 'Type',
valueField: 'Price'
}));
如果要處理浮動(dòng)值...
您可以使用parseFloat代替parseInt和格式化數(shù)字toFixed(2)。
const array = [
{ Type: "Food", Price: "100" },
{ Type: "Entertainment", Price: "200" },
{ Type: "Food", Price: "80" },
{ Type: "Entertainment", Price: "150" }
];
function calculateTotal(data, key, options) {
let opts = Object.assign({ keyField : 'key', valueField : 'value' }, options || {});
return data.reduce((sum, item) => {
return sum + (item[opts.keyField] === key ? parseFloat(item[opts.valueField]) : 0);
}, 0);
}
console.log('Total cost of Food: $' + calculateTotal(array, 'Food', {
keyField : 'Type',
valueField : 'Price'
}).toFixed(2));

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
使用forEach()迭代數(shù)組并添加給定的價(jià)格type以獲得總數(shù)。
使用Number()將價(jià)格從字符串轉(zhuǎn)換為數(shù)字。即使價(jià)格有小數(shù),這也有效。
const array = [ { Type: "Food", Price: "100" }, { Type: "Entertainment", Price: "200" }, { Type: "Food", Price: "80" }, { Type: "Entertainment", Price: "150" } ];
function getTotalPrice(array, type) {
let total = 0;
array.forEach(item => {
if (item.Type === type) {
total += Number(item.Price);
}
});
return total;
}
console.log(getTotalPrice(array, "Food"));
或者使用reduce()。
const array = [ { Type: "Food", Price: "100" }, { Type: "Entertainment", Price: "200" }, { Type: "Food", Price: "80" }, { Type: "Entertainment", Price: "150" } ];
function getTotalPrice(array, type) {
return array.reduce((total, item) => {
if (item.Type === type) {
total += Number(item.Price);
}
return total;
}, 0);
return total;
}
console.log(getTotalPrice(array, "Food"));
添加回答
舉報(bào)