我真的需要一些幫助來解決我收到的家庭作業問題。我不想要這個問題的答案,但我想知道使用什么方法方面的一些幫助。
我的問題是:
您的開發團隊的產品負責人相信他們已經看到了一種模式,即哪些客戶購買的襪子最多。為了驗證,您被要求撰寫一個函式來處理客戶物件陣列并回傳一個新陣列,該陣列僅包含符合以下任何條件的客戶:
- 名稱以“C”開頭(大寫或小寫)
- 地址不包含未定義的欄位
- 城市是皮奧里亞,州是亞利桑那州
- 會員級別是 GOLD 或 PLATINUM 除非客戶小于 29 歲,那么 SILVER 也可以
客戶物件陣列將具有以下架構:
const customers = [
{
name: 'Sam',
address: {
street: '1234 W Bell Rd',
city: 'Phoenix',
zip: '85308',
state: 'AZ'
},
membershipLevel: 'GOLD',
age: 32
},
//more customers with the same schema
];
注意:此問題的解決方案不需要使用回呼。您還需要使用點符號來訪問屬性。例如,customers[0].name 將回傳“Sam”。
如您所見,我必須通過幾個涉及字串和部分字串的不同引數對這個陣列進行排序。但是,我嘗試使用典型的 Array.sort() 方法但失敗了,因為我實際上是在對物件進行排序,并且我找不到按字串的一部分對物件進行排序的方法。此外,我需要幫助將其全部保存在一個單獨的陣列中。
我認為對我幫助最大的事情是給我應該為上述問題中的每個標準使用的方法。如果是這樣,我可以查找并希望自己學習這些方法。我愿意接受任何建議。感謝您花時間閱讀本文并幫助我。
uj5u.com熱心網友回復:
您可以借助以下方法實作此要求:
Array.filter()- 根據客戶陣列中的過濾條件過濾出所需的客戶。Object.values()- 由于address欄位是一個物件,我們可以通過使用檢查未定義的值Object.values(),然后通過使用檢查未定義的值Array.some()String.startsWith()- 檢查字串是否以指定字串的字符開頭,根據需要回傳 true 或 false。Conditional (Ternary) operator-membership根據方法age來處理Array.includes()。
現場演示:
const customers = [
{
name: 'Sam',
address: {
street: '1234 W Bell Rd',
city: 'Phoenix',
zip: '85308',
state: 'AZ'
},
membershipLevel: 'GOLD',
age: 32
},
{
name: 'Carlo',
address: {
street: '1250 Ring Rd',
city: 'Peoria',
zip: '85308',
state: 'AZ'
},
membershipLevel: 'SILVER',
age: 28
}
];
const res = customers.filter(customer => {
const nameCondition = customer.name.startsWith('C');
const addressCondition = !Object.values(customer.address).some(val => !val);
const cityStateCondition = customer.address.city === 'Peoria' && customer.address.state === 'AZ';
const membershipCondition = (customer.age > 29) ? ['GOLD', 'PLATINUM'].includes(customer.membershipLevel) : ['GOLD', 'PLATINUM', 'SILVER'].includes(customer.membershipLevel)
return nameCondition && addressCondition && cityStateCondition && membershipCondition;
});
console.log(res);
uj5u.com熱心網友回復:
您可以使用array#filter回傳符合您要求的元素陣列
filter()方法創建給定陣列的一部分的淺表副本,過濾到給定陣列中通過所提供函式實作的測驗的元素。
第一個引數是一個測驗陣列每個元素的函式。回傳一個強制保留元素的值,
true或者強制保留元素false。
示例 1
//REM: name starts with a 'C' (upper or lowercase)
console.log(
[
{
name: 'Sam',
address: {
street: '1234 W Bell Rd',
city: 'Phoenix',
zip: '85308',
state: 'AZ'
},
membershipLevel: 'GOLD',
age: 32
},
{
name: 'Caludia',
address: {
street: '1234 W Bell Rd',
city: 'Phoenix',
zip: '85308',
state: 'AZ'
},
membershipLevel: 'GOLD',
age: 32
},
].filter(item => {
return item.name.startsWith('c') || item.name.startsWith('C')
})
)
示例 2
//REM: name starts with a 'C' (upper or lowercase)
//REM: the city is Peoria and the state is AZ
console.log(
[
{
name: 'Sam',
address: {
street: '1234 W Bell Rd',
city: 'Phoenix',
zip: '85308',
state: 'AZ'
},
membershipLevel: 'GOLD',
age: 32
},
{
name: 'Caludia',
address: {
street: '1234 W Bell Rd',
city: 'Peoria',
zip: '85308',
state: 'AZ'
},
membershipLevel: 'GOLD',
age: 32
},
].filter(item => {
return (
(item.name.startsWith('c') || item.name.startsWith('C')) &&
(item.address?.city === 'Peoria') &&
(item.address?.state === 'AZ')
)
})
)
只需逐步添加您的四個要求中的每一個,直到達到目標。如果結構一致,則要求2的一半已包含在要求3 中。
uj5u.com熱心網友回復:
您可以使用過濾器方法創建一個滿足您列出的條件的新物件陣列。盡管您可以通過直接在其中撰寫自己的函式來使用過濾器,但您也有另一種選擇,您可以撰寫相同的函式并將其作為引數傳遞給過濾器方法,這有時在簡單性和可維護性方面可能會更好。下面是檢查物件是否滿足條件的函式。
第一種方法
const customers = [
{
name: "Sam",
address: {
street: "1234 W Bell Rd",
city: "Phoenix",
zip: "85308",
state: "AZ",
},
membershipLevel: "GOLD",
age: 32,
},
{
name: "Cindy",
address: {
street: "1286834 E Bull Rd",
city: "Peoria",
zip: "85308",
state: "AZ",
},
membershipLevel: "GOLD",
age: 32,
},
{
name: "Chloe",
address: {
street: "1234 S Dull Rd",
city: "Peoria",
zip: "85308",
state: "AZ",
},
membershipLevel: "SILVER",
age: 25,
},
{
name: "Charlie",
address: {
street: "1234 N Dell Rd",
city: "Peoria",
zip: "85308",
state: "VA",
},
membershipLevel: "SILVER",
age: 20,
},
];
// check if a customer satisfies all conditions
function checkCustomer(customerObject) {
// object destructuring
const {
name,
address: { city, state },
membershipLevel,
age,
} = customerObject;
if (
(name.startsWith("c") || name.startsWith("C")) &&
city === "Peoria" &&
state === "AZ" &&
((age >= 29 &&
(membershipLevel === "GOLD" || membershipLevel === "PLATINUM")) ||
(age < 29 && membershipLevel === "SILVER"))
) {
return true;
}
return false;
}
// write names of customers that satisfy conditions
customers.filter(checkCustomer).forEach(customer => {
console.log(customer.name);
})
但是,最好不要撰寫難以閱讀和使用的 if 陳述句塊。因此,您可以通過將條件分成更小的部分并分別檢查它們來撰寫相同的函式,這樣您就可以輕松地消除事物。下面是相同的函式,但如果所有測驗(可以這么說)都通過,它會回傳 true。
第二種方法
const customers = [
{
name: "Sam",
address: {
street: "1234 W Bell Rd",
city: "Phoenix",
zip: "85308",
state: "AZ",
},
membershipLevel: "GOLD",
age: 32,
},
{
name: "Cindy",
address: {
street: "1286834 E Bull Rd",
city: "Peoria",
zip: "85308",
state: "AZ",
},
membershipLevel: "GOLD",
age: 36,
},
{
name: "Chloe",
address: {
street: "1234 S Dull Rd",
city: "Peoria",
zip: "85308",
state: "AZ",
},
membershipLevel: "SILVER",
age: 25,
},
{
name: "Charlie",
address: {
street: "1234 N Dell Rd",
city: "Peoria",
zip: "85308",
state: "VA",
},
membershipLevel: "SILVER",
age: 20,
},
];
// check if a customer satisfies all conditions
function checkCustomer(customerObject) {
// object destructuring
const {
name,
address: { city, state },
membershipLevel,
age,
} = customerObject;
// name should begin with c (uppercase or lowercase)
if (!(name.startsWith("c") || name.startsWith("C"))) {
return false;
}
// address info can not contain empty field
for (const addressKey in customerObject.address) {
if (customerObject.address[addressKey] === "") {
return false;
}
}
// city and state should match
if (!(city === "Peoria" && state === "AZ")) {
return false;
}
// membership level can be gold or platinum if age >= 29
// otherwise silver should suffice
if (
!(
(age >= 29 &&
(membershipLevel === "GOLD" || membershipLevel === "PLATINUM")) ||
(age < 29 && membershipLevel === "SILVER")
)
) {
return false;
}
return true;
}
// write names of customers that satisfy conditions
customers.filter(checkCustomer).forEach(customer => {
console.log(customer.name);
})
順便說一句,您列出的第二個條件,地址不包含未定義的欄位,不清楚。在 javascript 中,undefined 具有特殊含義,它是您在嘗試使用變數而不分配值時看到的原始型別之一。在這種情況下,如果您嘗試使用不存在的鍵(即 customer[0].address.houseNumber)訪問物件,您將看到未定義。因此,我在函式中添加了一個檢查地址資訊是否為空的部分。如果要檢查函式的變數或回傳型別是否未定義,可以使用嚴格相等。
最后,您可以通過鍵檢查物件的排序陣列,以查看如何對陣列進行排序。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/528711.html
上一篇:創建一個名為“scorers”的物件,其中包含作為屬性得分的球員的姓名
下一篇:當值為陣列時獲取鍵
