我有两个数组 if "Cost" 和 "Sell" 价格关于许多水果。如何使用 "smart" 函数过滤器和映射计算差异?
I have two arrays if "Cost" and "Sell" price about many fruits. How calculate the diference using a "smart" function filter and map?
in Vue3 composition API, I have 2 arrays
const fruitsSellerPrice = [
{id: 1, name: apple, priceSeller: '10.90'},
{id: 2, name: banana, priceSeller: '11.20'},
{id: 3, name: orange, priceSeller: '12.30'},
{id: 4, name: framboesa, priceSeller: '13.90'}}];
const fruitsBuyPrice = [
{id: 1, name: apple, priceBuy: '6.90'},
{id: 2, name: banana, priceBuy: '6.20'},
{id: 3, name: orange, priceBuy '7.30'},
{id: 4, name: framboesa, priceBuy: '16.90'}}];
我想知道我怎样才能做一个“智能”过滤器,return 通过“名称”区分并从高到低或从低到高排序,priceSell - PriceBuy...
我有一个函数有很多行..出了点问题....
// find all seller items, and check what the value is in the buy array is and respond with the difference.
const response = fruitsSellerPrice.map( item =>
({ ...item,
diff:
Math.abs(
Number(
fruitsBuyPrice.find(
buy=>buy.name === item.name).priceBuy)
- Number(item.priceSeller).toFixed(2))
})
).sort((a,b)=>b.diff-a.diff)
// for asc, do a.diff - b.diff
尝试类似以下代码段的内容:
const fruitsSellerPrice = [{id: 1, name: 'apple', priceSeller: '10.90'},{id: 2, name: 'banana', priceSeller: '11.20'},{id: 3, name: 'orange', priceSeller: '12.30'},{id: 4, name: 'framboesa', priceSeller: '13.90'}]
const fruitsBuyPrice = [{id: 1, name: 'apple', priceBuy: '6.90'},{id: 2, name: 'banana', priceBuy: '6.20'},{id: 3, name: 'orange', priceBuy: '7.30'},{id: 4, name: 'framboesa', priceBuy: '16.90'}]
let sortOrder = true
const res = fruitsSellerPrice.map(f => {
return {...f, diff: Math.abs((Number(f.priceSeller) - Number(fruitsBuyPrice.find(u => u.id === f.id).priceBuy))).toFixed(2)}
}).sort((a, b) => {
return sortOrder ? a.diff - b.diff : b.diff - a.diff
})
console.log(res)
in Vue3 composition API, I have 2 arrays
const fruitsSellerPrice = [
{id: 1, name: apple, priceSeller: '10.90'},
{id: 2, name: banana, priceSeller: '11.20'},
{id: 3, name: orange, priceSeller: '12.30'},
{id: 4, name: framboesa, priceSeller: '13.90'}}];
const fruitsBuyPrice = [
{id: 1, name: apple, priceBuy: '6.90'},
{id: 2, name: banana, priceBuy: '6.20'},
{id: 3, name: orange, priceBuy '7.30'},
{id: 4, name: framboesa, priceBuy: '16.90'}}];
我想知道我怎样才能做一个“智能”过滤器,return 通过“名称”区分并从高到低或从低到高排序,priceSell - PriceBuy...
我有一个函数有很多行..出了点问题....
// find all seller items, and check what the value is in the buy array is and respond with the difference.
const response = fruitsSellerPrice.map( item =>
({ ...item,
diff:
Math.abs(
Number(
fruitsBuyPrice.find(
buy=>buy.name === item.name).priceBuy)
- Number(item.priceSeller).toFixed(2))
})
).sort((a,b)=>b.diff-a.diff)
// for asc, do a.diff - b.diff
尝试类似以下代码段的内容:
const fruitsSellerPrice = [{id: 1, name: 'apple', priceSeller: '10.90'},{id: 2, name: 'banana', priceSeller: '11.20'},{id: 3, name: 'orange', priceSeller: '12.30'},{id: 4, name: 'framboesa', priceSeller: '13.90'}]
const fruitsBuyPrice = [{id: 1, name: 'apple', priceBuy: '6.90'},{id: 2, name: 'banana', priceBuy: '6.20'},{id: 3, name: 'orange', priceBuy: '7.30'},{id: 4, name: 'framboesa', priceBuy: '16.90'}]
let sortOrder = true
const res = fruitsSellerPrice.map(f => {
return {...f, diff: Math.abs((Number(f.priceSeller) - Number(fruitsBuyPrice.find(u => u.id === f.id).priceBuy))).toFixed(2)}
}).sort((a, b) => {
return sortOrder ? a.diff - b.diff : b.diff - a.diff
})
console.log(res)