這是我的代碼
https://codepen.io/hobbesa/pen/jOGREKy?editors=1111
我試圖獲得僅作業日的價格。我該怎么做?
this.getPriceWeekDay = this.StrategypricingDetail.map((rec) =>
rec.map((daysOfWeek) => {
return daysOfWeek;
}),
uj5u.com熱心網友回復:
您可以首先過濾不包含Saturday或Sunday使用filter和find方法的專案,然后通過map方法遍歷過濾后的串列以僅提取 price 屬性,如下所示:
let StrategypricingDetail = [ { id: 3, name: "test", price: 30, daysOfWeek: [ { id: 1, name: "Monday" }, { id: 2, name: "Tuesday" }, { id: 3, name: "Wednesday" } ] }, { id: 23, name: "Testing2", price: 10, daysOfWeek: [ { id: 1, name: "Monday" }, { id: 2, name: "Tuesday" } ] }, { id: 13, name: "Testing3", price: 14, daysOfWeek: [ { id: 1, name: "Saturaday" }, { id: 2, name: "Sunday" } ] } ];
const weekDaysPrice = StrategypricingDetail.filter(({daysOfWeek}) => !daysOfWeek.find(({name}) => name == 'Saturaday' || name == 'Sunday')).map(({price}) => price);
console.log(weekDaysPrice);
uj5u.com熱心網友回復:
您可以在下面使用一種解決方案。
let strategypricingDetail = [
{
id: 3,
name: "test",
price: 30,
daysOfWeek: [
{ id: 1, name: "Monday" },
{ id: 2, name: "Tuesday" },
{ id: 3, name: "Wednesday" }
]
},
{
id: 23,
name: "Testing2",
price: 10,
daysOfWeek: [
{ id: 1, name: "Monday" },
{ id: 2, name: "Tuesday" }
]
},
{
id: 13,
name: "Testing3",
price: 14,
daysOfWeek: [
{ id: 1, name: "Saturaday" },
{ id: 2, name: "Sunday" }
]
}
];
let finalList = [];
// Create list of eligible days for the search
let daysToInclude = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
];
strategypricingDetail.forEach((item, index) => {
for (let i = 0; i < item.daysOfWeek.length; i ) {
// Check if the day is in the eligible list
if (daysToInclude.includes(item.daysOfWeek[i].name)) {
finalList.push(item);
break;
// When found, break out of the search after adding it to the final list.
}
}
});
console.log(finalList)
在此解決方案中,您遍歷可用專案,然后將每個專案的“daysOfWeel”串列與符合條件的日期串列進行比較。一旦找到,它就會停止搜索,并將該專案添加到新串列中,直到您以適當日期的串列結束。
uj5u.com熱心網友回復:
//StrategypricingDetail is the whole json
const StrategypricingDetail = [
{
id: 3,
name: "test",
price: 30,
daysOfWeek: [
{ id: 1, name: "Monday" },
{ id: 2, name: "Tuesday" },
{ id: 3, name: "Wednesday" }
]
},
{
id: 23,
name: "Testing2",
price: 10,
daysOfWeek: [
{ id: 1, name: "Monday" },
{ id: 2, name: "Tuesday" }
]
},
{
id: 13,
name: "Testing3",
price: 14,
daysOfWeek: [
{ id: 1, name: "Saturaday" },
{ id: 2, name: "Sunday" }
]
}
];
const weekends = ["Saturaday","Sunday"]
// Approach
// we have to filter some element based on condition
// condition will be days should not belong to weekends
this.getPriceWeekDay = StrategypricingDetail.filter((rec) => {
for(let dayIndex = 0 ; dayIndex < rec.daysOfWeek.length; dayIndex ){
const dayName = rec.daysOfWeek[dayIndex].name;
if(weekends.includes(dayName)){
return;
}
}
return rec;
}).map(({price}) => price);
console.log(getPriceWeekDay);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414919.html
標籤:
