显示书籍的平均价格



我需要一些代码来计算书籍的平均价格,类型为ROMAN。类型字段可以在任何情况下书写(罗马、罗马)。

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

此外,我的问题是,我不知道如何获得价格

您也可以使用filterreduce

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)

您可以在几种方法中做到这一点

您可以在数组中循环,将所有价格相加,然后根据其长度进行细分,还可以使用.toLowerCase()检查当前对象类型是否等于roman

如果你想返回自然数,你可以使用.toFixed()方法

For Loop

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

使用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))

正如@Barmar所建议的,您可以使用简单的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
}
];
var total = 0,
romans = books.filter(book => book.type.toLowerCase() === 'roman');
for (var roman of romans) total += roman.price;
var average = (total / romans.length).toFixed(2);
console.log(average);

尽管已经有点太晚了,但以下是我的答案。我的解决方案将这些类型放在一起,并为您提供一个具有平均价格、计数和总和的对象。

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 }
];
// find categories
index = books.map(item => item.type.toLowerCase() ).filter((value, index, self) => self.indexOf(value) === index)
// init category object with parameters: sum, count, average
const obj = {}
index.forEach(i => {
obj[i] = {sum: 0, count: 0, average: 0}
});
// iterate bookObject and calculate values
books.forEach(book => {
const sum = obj[book.type.toLowerCase()].sum + book.price
const count = obj[book.type.toLowerCase()].count + 1  
const average = sum / count

obj[book.type.toLowerCase()].sum = sum
obj[book.type.toLowerCase()].count = count
obj[book.type.toLowerCase()].average = average
});
console.log(obj)

如果要按类型计算项目的平均值,首先需要过滤原始列表。过滤完列表后,可以使用reducer函数对价格求和,然后除以过滤列表的长度。

const 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 }
];
// Round a number to a precision. Default 2 decimal places.
const round = (n, p = 2) => (e => Math.round(n * e) / e)(Math.pow(10, p));
// This is a "generic" average calculation
const calcAvg = (dataList, producer, predicate) =>
(filtered =>
filtered.reduce((sum, item) => sum + producer(item), 0) / filtered.length)
(predicate ? dataList.filter(predicate) : dataList);
// Computed average of books with type="roman" (case-insensitive)
const avg = calcAvg(
books, // list of items to filter on and compute average
({ price }) => price, // producer or data accessor
({ type }) => type.toLowerCase() === 'roman', // predicate or filter callback
); // 243.875
// Currency formatter
const cf = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
// Average price; rounded up to nearest cent
console.log(cf.format(round(avg))); // $243.88

这是一个代码高尔夫版本:

const 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 }
]
// 158 bytes
r=(n,p=2)=>(e=>((n*e)+.5|0)/e)(10**p);g=(l,a,p)=>(m=>m.reduce((s,i)=>s+a(i),0)/m.length)(p?l.filter(p):l);f=(l,t)=>g(l,o=>o.price,o=>o.type.toLowerCase()===t)
console.log(`$${r(f(books, 'roman'))}`) // $243.88

最新更新