简体   繁体   English

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

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

So I have an array of objects that looks like: 所以我有一个对象数组,看起来像:

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

I'm trying to write a function that would allow me to print something like 我正在尝试编写一个函数,使我可以打印类似

  • 5 a's costs $2.5 5 a的费用为$ 2.5
  • 2 b's costs $4 2 b的费用为$ 4
  • 1 c costs $2 1 c花费$ 2

  • The total was $8.50 总计$ 8.50

So far I have something like 到目前为止,我有类似

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

But it obviously doesn't work. 但这显然行不通。 I guess I'm a little confused as to how to go about how to properly access the values in those objects with my function. 我想我对如何使用我的函数正确访问那些对象中的值有些困惑。 Any input would be greatly appreciated! 任何投入将不胜感激!

You want to do something like this 你想做这样的事情

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

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

Your function can be made to work to return the string for one entry, not for list , so you should change that variable reference. 可以使您的函数工作以返回一个条目的字符串,而不返回list的字符串,因此您应该更改该变量引用。 You could use destructuring in the function parameter so you don't need any other variables or property references. 您可以在function参数中使用解构,因此不需要任何其他变量或属性引用。

Then you need to call that function for each element in list . 然后,您需要为list每个元素调用该函数。 This you can do with map , which will give you an array of strings. 您可以使用map此操作,这将为您提供字符串数组。 Finally, join that array into one string: 最后,将该数组连接到一个字符串中:

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

Here is the Classic 这是经典

( you will first need to fix the array of objects declaration here...) (您首先需要在此处修复对象声明的数组...)

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 }
];

Then you can move on to the task 然后您可以继续执行任务

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