我需要一些代码,实现计算图书的平均价格,罗马类型。类型字段可以以任何大小写形式写入(罗马、罗马)。
let books = [
{ name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
{ name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
{ name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
{ name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
{ name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
{ name: 'Some-6', author: 'Hohol', type: 'tale', price: 366.0 }
];所以我用这个方法派生了所有类型:
const getType = books.map(({type}) => type.toLowerCase())此外,我的问题是,我不知道如何获得价格
发布于 2021-09-14 21:05:47
有几种方法可以做到这一点
您可以遍历数组并对其中的所有价格求和,然后对其长度进行除法运算,还可以使用.toLowerCase()检查当前对象类型是否等于roman
如果想要返回自然数,可以使用.toFixed()方法
For循环
let books = [
{ name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
{ name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
{ name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
{ name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
{ name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
{ name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
]
let TotalPrice = 0;
let romanPriceLength = 0;
for (let i = 0; i < books.length; i++) {
if ((books[i].type).toLowerCase() === "roman") {
TotalPrice += books[i].price
romanPriceLength++
}
}
console.log(TotalPrice / romanPriceLength)
console.log((TotalPrice / romanPriceLength).toFixed(0))
forEach()
let books = [
{ name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
{ name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
{ name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
{ name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
{ name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
{ name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
]
let TotalPrice = 0;
let romanPriceLength = 0;
books.forEach(book => {
if((book.type).toLowerCase() === "roman"){
TotalPrice += book.price
romanPriceLength ++
}
})
console.log(TotalPrice / romanPriceLength)
console.log((TotalPrice / romanPriceLength ).toFixed(0))
为了..。的
let books = [
{ name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
{ name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
{ name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
{ name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
{ name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
{ name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
]
let romanPriceLength = 0;
let TotalPrice = 0;
for(let book of books){
if(book.type.toLowerCase() === "roman"){
TotalPrice += book.price
romanPriceLength++
}
}
console.log(TotalPrice / romanPriceLength)
console.log((TotalPrice / romanPriceLength).toFixed(0))
发布于 2021-09-14 21:08:51
使用for循环或forEach()来获取价格和计数的总和。
let books = [
{ name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
{ name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
{ name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
{ name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
{ name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
{ name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
]
let TotalPrice = 0;
let count = 0;
books.forEach(book => {
if (book.type.toLowerCase() == 'roman') {
TotalPrice += book.price;
count++;
}
});
console.log((TotalPrice / books.length).toFixed(2))
发布于 2021-09-14 21:30:36
你也可以用filter和reduce做到这一点:
let books = [
{ name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
{ name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
{ name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
{ name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
{ name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
{ name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
]
const romans = books.filter(book => book.type.toLowerCase() == 'roman')
const romansTotalPrice = romans.reduce((a, {price}) => a + price, 0)
const romansAvaragePrice = (romansTotalPrice / romans.length).toFixed(2);
console.log(romansAvaragePrice)
https://stackoverflow.com/questions/69184465
复制相似问题