admin管理员组文章数量:1406177
const data = [
{
id: 1,
title: "buttermilk pancakes",
category: "fullstack",
price: 15.99,
img: "./img/item-1.jpeg",
desc: `I'm baby woke mlkshk wolf bitters live-edge blue bottle, hammock freegan copper mug whatever cold-pressed `,
},
{
id: 2,
title: "diner double",
category: "backend",
price: 13.99,
img: "./img/item-2.jpeg",
desc: `vaporware iPhone mumblecore selvage raw denim slow-carb leggings gochujang helvetica man braid jianbing. Marfa thundercats `,
},
];
export default data;
Here i looped through the object using map but the returned result displays the iterated price sums. I also used the JavaScript function reduce and that did not work either.
const Test = () => {
let sum = 0;
return (
<div>
{data.map((items) => {
const values = items.price;
sum = values + sum;
console.log(Math.max(sum));
const { id } = items;
return (
<div key={id}>
<p>{sum}</p>
</div>
);
})}
</div>
);
};
How do I get the total value of the price using react?
const data = [
{
id: 1,
title: "buttermilk pancakes",
category: "fullstack",
price: 15.99,
img: "./img/item-1.jpeg",
desc: `I'm baby woke mlkshk wolf bitters live-edge blue bottle, hammock freegan copper mug whatever cold-pressed `,
},
{
id: 2,
title: "diner double",
category: "backend",
price: 13.99,
img: "./img/item-2.jpeg",
desc: `vaporware iPhone mumblecore selvage raw denim slow-carb leggings gochujang helvetica man braid jianbing. Marfa thundercats `,
},
];
export default data;
Here i looped through the object using map but the returned result displays the iterated price sums. I also used the JavaScript function reduce and that did not work either.
const Test = () => {
let sum = 0;
return (
<div>
{data.map((items) => {
const values = items.price;
sum = values + sum;
console.log(Math.max(sum));
const { id } = items;
return (
<div key={id}>
<p>{sum}</p>
</div>
);
})}
</div>
);
};
How do I get the total value of the price using react?
Share Improve this question edited Dec 26, 2021 at 2:13 Filip Seman 1,8042 gold badges22 silver badges29 bronze badges asked Dec 25, 2021 at 13:23 Brown Brown 735 bronze badges 1- Create a function which return the sum and call that function in code. – amardeep saini Commented Dec 25, 2021 at 13:27
4 Answers
Reset to default 4You can use reduce
function to achieve this
data.reduce((sum, p)=> sum+p.price, 0);
You can use it inside your ponent
const Test = ()=> {
return <div>{data.reduce((sum, p)=> sum+p.price, 0);}</div>
}
You can simply achieve the calculation of total price using forEach
instead of map
(as map has its own use case like creating new array, and forEach
is available for us just for this simple iteration like in your use case) with a one-liner like:
const data = [{
id: 1,
title: 'buttermilk pancakes',
category: 'fullstack',
price: 15.99,
img: './img/item-1.jpeg',
desc: `I'm baby woke mlkshk wolf bitters live-edge blue bottle, hammock freegan copper mug whatever cold-pressed `,
},
{
id: 2,
title: 'diner double',
category: 'backend',
price: 13.99,
img: './img/item-2.jpeg',
desc: `vaporware iPhone mumblecore selvage raw denim slow-carb leggings gochujang helvetica man braid jianbing. Marfa thundercats `,
}
];
var sum = 0;
data.forEach(subData => sum += subData.price);
console.log(sum) // return this `sum` value alone in your div tag below.
//return <div>{sum}</div>
Please refer this code, it may help your code
const Test = () => {
let sum = 0;
data.forEach((item) => {
sum = sum + item.price;
});
return(
<div> {sum} </div>
)
}
const Test = ()=> {
var prices = data.map(({price})=> price).reduce((a, b)=> a+b, 0);
return(
<div>
{prices}
</div>
)
}
thank you everyone, I also tried this and it worked.
本文标签:
版权声明:本文标题:javascript - How do i iterate over an array of object in react.js and add the values of prices in the object as shown below - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744971528a2635261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论