簡體   English   中英

創建一個處理Javascript中的對象數組的函數

[英]Creating a function that deals with an array of objects in Javascript

所以我有一個對象數組,看起來像:

list = [firstObject {name: 'a', price: 0.5, quantity: 5, total: 2.5},
        secondObject {name: 'b', price: 2, quantity: 2, total: 4},
        thirdObject {name: 'd', price: 2, quantity: 1, total: 2}]

我正在嘗試編寫一個函數,使我可以打印類似

  • 5 a的費用為$ 2.5
  • 2 b的費用為$ 4
  • 1 c花費$ 2

  • 總計$ 8.50

到目前為止,我有類似

let summary = (name, quantity, total) => {
 let item = list.name;
 let amount = list.quantity;
 let cost = list.total;
  return (`${amount} ${item} costs ${cost}`);
};

但這顯然行不通。 我想我對如何使用我的函數正確訪問那些對象中的值有些困惑。 任何投入將不勝感激!

你想做這樣的事情

list = [{name: 'a', price: 0.5, quantity: 5, total: 2.5},
        {name: 'b', price: 2, quantity: 2, total: 4},
        {name: 'd', price: 2, quantity: 1, total: 2}];

function sum(list){
   let total = 0;

   list.forEach((item)=>{
      console.log(item.quantity + ' ' + item.name + ' costs ' + (item.price * item.quantity));
       total += item.price * item.quantity;
   }
   return total;
});
console.log('Total: ' + sum(list));

試試這個片段

<script>
let summary = (name, quantity, total) => {
    let item = name;
    let amount = quantity;
    let cost = total;
    return (`${amount} ${item} costs ${cost}`);
};
var list = [firstObject = {name: 'a', price: 0.5, quantity: 5, total: 2.5},
           secondObject = {name: 'b', price: 2, quantity: 2, total: 4},
           thirdObject = {name: 'd', price: 2, quantity: 1, total: 2}];

for (var item = 0; item < list.length; item++) {
    summary(list[item].name, list[item].quantity, list[item].total);
}
</script>

可以使您的函數工作以返回一個條目的字符串,而不返回list的字符串,因此您應該更改該變量引用。 您可以在function參數中使用解構,因此不需要任何其他變量或屬性引用。

然后,您需要為list每個元素調用該函數。 您可以使用map此操作,這將為您提供字符串數組。 最后,將該數組連接到一個字符串中:

 const list = [{name: 'a', price: 0.5, quantity: 5, total: 2.5}, {name: 'b', price: 2, quantity: 2, total: 4}, {name: 'd', price: 2, quantity: 1, total: 2}]; const summary = ({name, quantity, total}) => `${quantity} ${name}'s cost $${total}`; const output = list.map(summary).join('\\n'); console.log(output); // Code to display the total: const total = list.reduce((sum, {total}) => sum + total, 0); console.log(`The total is $${total}`); 

list = [{name: 'a', price: 0.5, quantity: 5, total: 2.5},
    {name: 'b', price: 2, quantity: 2, total: 4},
    {name: 'd', price: 2, quantity: 1, total: 2}]

function display (obj) {
    console.log(obj.quantity + " " + obj.name +  "'s costs $" + obj.total);
};

let total = 0;
list.forEach(function(obj) {
    display(obj);
    total += obj.total;
});

console.log("The total was ", total);

這是經典

(您首先需要在此處修復對象聲明的數組...)

list = 
[
   { name: 'a', price: 0.5, quantity: 5, total: 2.5 },
   { name: 'b', price: 2, quantity: 2, total: 4 },
   { name: 'c', price: 2, quantity: 1, total: 2 }
];

然后您可以繼續執行任務

var total = 0;
 for( var item in list )console.log(
       list[ item ].quantity + " " + list[ item ].name + "\'s cost $" + list[item].total
    ), total += list[ item ].total;
console.log( "The total was $"+ total );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM