reactjs 获取数组中字段的最小值javascript

c3frrgcw  于 2023-01-01  发布在  React
关注(0)|答案(3)|浏览(112)

我有一个项目的数组,我只想提取最便宜的产品版本.例如,我有两个iPhone XS,但我只想提取便宜的一个,并添加到另一个数组中的所有最便宜的项目.

const [items,setItems] = useState([
    {"itemid":"001" , "Product":"Iphone XS MAX PRO 512GB","Price" : 1200},
    {"itemid":"002" , "Product":"Samsung Galaxy S20 128GB","Price" : 700},
    {"itemid":"003" , "Product":"Huwaei x8 ","Price" : 600},
    {"itemid":"004" , "Product":"OPPO","Price" : 400},
    {"itemid":"005" , "Product":"Nokia","Price" : 200},
    {"itemid":"006" , "Product":"OPPO","Price" : 500},  
    {"itemid":"007" , "Product":"Iphone XS MAX PRO 512GB","Price" : 900},
  ])

正如你所看到的有两个Oppo项目和两个iPhone项目。我需要得到的只有最便宜的版本了他们两个和其余的其他项目也。我该怎么做呢?
如有任何建议,我们将不胜感激

46scxncf

46scxncf1#

const data = [
  {"itemid":"001" , "Product":"Iphone XS MAX PRO 512GB","Price" : 1200},
  {"itemid":"002" , "Product":"Samsung Galaxy S20 128GB","Price" : 700},
  {"itemid":"003" , "Product":"Huwaei x8 ","Price" : 600},
  {"itemid":"004" , "Product":"OPPO","Price" : 400},
  {"itemid":"005" , "Product":"Nokia","Price" : 200},
  {"itemid":"006" , "Product":"OPPO","Price" : 500},  
  {"itemid":"007" , "Product":"Iphone XS MAX PRO 512GB","Price" : 900},
];

const filtered = data.filter(item => /\biphone xs\b/i.test(item.Product));
const result = filtered.reduce((prev, curr) => prev.Price < curr.Price ? prev : curr);

console.log(result);

要创建一个可重用函数,通过提供要搜索的数组和产品名称来返回最便宜的商品(Price),请执行以下操作:

// Utility function to escape regex sensitive characters
const regEscape = (str) => str.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');

// Get cheapest product by Price given a Product name from array of products:
const getCheapest = (products, name) => {
  const filtered = products.filter(item => new RegExp(`\\b${regEscape(name)}\\b`, "i").test(item.Product));
  const result = filtered.reduce((prev, curr) => prev.Price < curr.Price ? prev : curr);
  return result;
};

const data = [
  {"itemid":"001" , "Product":"Iphone XS MAX PRO 512GB","Price" : 1200},
  {"itemid":"002" , "Product":"Samsung Galaxy S20 128GB","Price" : 700},
  {"itemid":"003" , "Product":"Huwaei x8 ","Price" : 600},
  {"itemid":"004" , "Product":"OPPO","Price" : 400},
  {"itemid":"005" , "Product":"Nokia","Price" : 200},
  {"itemid":"006" , "Product":"OPPO","Price" : 500},  
  {"itemid":"007" , "Product":"Iphone XS MAX PRO 512GB","Price" : 900},
];

console.log(getCheapest(data, "iphone xs"));
6ju8rftf

6ju8rftf2#

我不知道react或者useState()是做什么的,但是如果你正在寻找一种方法来为每个产品获得“最便宜”的选择,那么下面的方法可能会很有帮助:

const cheapest=Object.values([
{"itemid":"001" , "Product":"Iphone XS MAX PRO 512GB","Price" : 1200},
{"itemid":"002" , "Product":"Samsung Galaxy S20 128GB","Price" : 700},
{"itemid":"003" , "Product":"Huwaei x8 ","Price" : 600},
{"itemid":"004" , "Product":"OPPO","Price" : 400},
{"itemid":"005" , "Product":"Nokia","Price" : 200},
{"itemid":"006" , "Product":"OPPO","Price" : 500},  
{"itemid":"007" , "Product":"Iphone XS MAX PRO 512GB","Price" : 900},
  ].reduce((a,c)=>{
   a[c.Product]??=c;
   if (c.Price<a[c.Product].Price) 
    a[c.Product]=c;
   return a;}
   ,{}));

console.log(JSON.stringify(cheapest));
xxhby3vn

xxhby3vn3#

按价格升序排序,创建一个不包含现有项目的新数组。

[...items]
.sort((a, b) => (a.price > b.price ? 1 : -1))
.reduce((acc, x) =>
acc.filter(y => y.Product == x.Product).length ? acc : [...acc, x], []);

相关问题