0

I want to calculate the cost and i am stuck from past 1 hour.Here is my data

Todo: [{
    name: "primary",
    items: [{
        item: 'Todo itme #1',
        isDone: false,
        cost: 0
    }]
}, {
    name: "Secondary",
    items: [{
        item: 'Todo itme #1',
        isDone: false,
        cost: 0
    }]
}]

I want to loop on all items and compute the total cost.

  • I have tried using for loops
  • using map within a map

but I am not able so solve it.

Entries inside items array can increase dynamically.

Please guide!

3 Answers 3

3

Array#reduce solution.

let Todo = [{
  name: "primary",
  items: [{
    item: 'Todo itme #1',
    isDone: false,
    cost: 3
  }, {
    item: 'Todo itme #2',
    isDone: false,
    cost: 2
  }]
}, {
  name: "Secondary",
  items: [{
    item: 'Todo itme #3',
    isDone: false,
    cost: 1
  }]
}], 
   totalCost = Todo.reduce((s,a) => {
     return s + a.items.reduce((s,a) => s + a.cost, 0);
   }, 0);
  
  console.log(totalCost);

Sign up to request clarification or add additional context in comments.

Comments

2

You just want to calculate the result of the sum? Then try this:

var sum = 0;
var Todo = [{
  name: "primary",
  items: [{
    item: 'Todo itme #1',
    isDone: false,
    cost: 35
  }]
}, {
  name: "Secondary",
  items: [{
    item: 'Todo itme #1',
    isDone: false,
    cost: 10
  }]
}];

Todo.forEach(function(todo) {
  todo.items.forEach(function(item) {
    sum += item.cost
  });
});

console.log(sum)

2 Comments

thanks a lot Chris.Working fine:) but i was doing the same thing with map instead of forEach and it did not work.Why so?
thanks a lot Chris.Working fine:) but i was doing the same thing with map instead of forEach and it did not work.Why so?
0

this code will not cost time to use it and search for it, I use it always

componentDidMount() {
    let i=0;
    let cost = 0;
    let Todo = [
        {name:"primary",items:[{item:'Todo itme #1',isDone:false,cost:20}]},
        {name:"Secondary",items:[{item:'Todo itme #2',isDone:false,cost:5}]},
    ];
    Todo.map(function( itemElement ){
        let itemElement2 = itemElement.name;
        let item =itemElement.items;
        return [itemElement2, item];
    }).filter(function(value){
        if (typeof value[0] === 'string'){
            console.log( "name " +value[0]  )
        }
        if(typeof value[1] === "object") {
            console.log(
                "item in items : item " + i +" "+
                value[1][0].item + " "+
                value[1][0].isDone + " "+
                value[1][0].cost
            );
            cost+=parseInt(value[1][0].cost);
            console.log(' cost '+cost)
        }
        return value ;
    });

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.