0

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
  • 2 b's costs $4
  • 1 c costs $2

  • The total was $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!

  • array items are accessed using an ordinal index ... 0, 1 or 2 in your case because you have three ... so `list[0].name` etc - note: your `list` variable is not valid syntax – Jaromanda X Aug 19 '17 at 00:51
  • Related: [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/q/11922383/218196) – Felix Kling Aug 19 '17 at 01:01

5 Answers5

0

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));
Jerinaw
  • 5,260
  • 7
  • 41
  • 54
0

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>
Louay Hamada
  • 781
  • 1
  • 14
  • 22
0

Your function can be made to work to return the string for one entry, not for list, so you should change that variable reference. You could use destructuring in the function parameter so you don't need any other variables or property references.

Then you need to call that function for each element in list. This you can do with map, which will give you an array of strings. 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}`);
trincot
  • 317,000
  • 35
  • 244
  • 286
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);
Raman
  • 44
  • 7
  • I went with your input and it works great! Thank you for your reply! – awesome_treehouse Aug 19 '17 at 01:39
  • YW. You got the idea right? all I did was 1) Iterate over the array 2) For every object in array, 2.1) Print the values in desired form. – Raman Aug 19 '17 at 02:45
  • Yep it's nice and straightforward. The only thing I'm having trouble with is how I might alter that function to output something like "Hello, here is your list:" and then for that item list to print, followed by "The total was, " total". Right now when I try to include the first message it prints for every iteration. Any ideas? – awesome_treehouse Aug 20 '17 at 12:51
  • If you put your console.log() statement outside of the loop, then it should work. For eg: function display (obj) { console.log(obj.quantity + " " + obj.name + "'s costs $" + obj.total); }; console.log("Hello, here is your list:"); let total = 0; list.forEach(function(obj) { display(obj); total += obj.total; }); console.log("The total was ", total); Notice that both of the console statements were put outside of the for loop. Let me know if you have any other doubt. – Raman Aug 20 '17 at 18:24
0

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 );
Community
  • 1
  • 1
Bekim Bacaj
  • 5,707
  • 2
  • 24
  • 26