I have an array of items in which i want to extract only the cheapest version of that product . e.g i have two iphones XS but i want to only extract the cheaper one and add all the cheapest items into another array . Below is items .
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},
])
As you can see there are two Oppo items and two iphone items . I need to get only the cheapest version out of both of them and for the rest of the other items also.How would i do this ?
Any suggestions will be appreciated
CodePudding user response:
- Filter by Product name using RegExp.prototype.test()
- Reduce the filtered results by
Price
property using Array.prototype.reduce()
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);
To create a reusable function that returns the cheapest item (by Price
) by providing an array to search and a product name:
// 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"));
CodePudding user response:
I don't know anything about react or what useState()
does, but if you are looking for a way to get the "cheapest" choice for each product then the following might be helpful:
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));
CodePudding user response:
Sort by price in ascending order, create a new array without already-existing items.
[...items]
.sort((a, b) => (a.price > b.price ? 1 : -1))
.reduce((acc, x) =>
acc.filter(y => y.Product == x.Product).length ? acc : [...acc, x], []);