第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

只推送數(shù)組中的一種類型的項(xiàng)目

只推送數(shù)組中的一種類型的項(xiàng)目

qq_笑_17 2021-12-12 10:56:25
我希望能夠從我的數(shù)組中提取在一個(gè)特定位置購(gòu)買的物品的價(jià)值。我希望能夠提取該值并將其放入一個(gè)單獨(dú)的數(shù)組中,例如在下面發(fā)布的數(shù)組中,我希望能夠僅獲取項(xiàng)目類型 Food 的價(jià)格并將其推送到一個(gè)單獨(dú)的數(shù)組中,如何才能我這樣做?const array = [    { Type: "Food", Price: "100" },    { Type: "Entertainment", Price: "200" },    { Type: "Food", Price: "80" },    { Type: "Entertainment", Price: "150" }];有沒(méi)有更簡(jiǎn)單的方法來(lái)獲得類型 Food 的總價(jià)?
查看完整描述

3 回答

?
月關(guān)寶盒

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


查看完整回答
反對(duì) 回復(fù) 2021-12-12
?
幕布斯6054654

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


查看完整回答
反對(duì) 回復(fù) 2021-12-12
?
幕布斯7119047

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"));


查看完整回答
反對(duì) 回復(fù) 2021-12-12
  • 3 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)