创建一个处理Javascript中的对象数组的函数

时间:2017-08-19 00:49:38

标签: javascript arrays function object

所以我有一个看起来像的对象数组:

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' s花费$ 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}`);
};

但它显然不起作用。我想我对如何使用我的函数正确访问这些对象中的值感到困惑。任何投入将不胜感激!

5 个答案:

答案 0 :(得分:0)

你想做这样的事情

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

答案 1 :(得分:0)

试试这个snippt

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

答案 2 :(得分:0)

您的函数可以用于返回一个条目的字符串,而不是list,因此您应该更改该变量引用。您可以在函数参数中使用destructuring,因此您不需要任何其他变量或属性引用。

然后你需要为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}`);

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这是Classic

(首先需要修改对象数组声明...)

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