我試圖從freecodecamp做以下挑戰:https ://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou我有幾個問題關于它。
- 為什么我的嘗試在我的本地控制臺上作業,而不是在 freecodecamp 上?意思是,在所有測驗中,4 個中有 3 個在我的控制臺中是正確的,但沒有一個在 FCC 上。
whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })如果其他所有測驗都通過,為什么這個測驗不通過?
我的預期結果嘗試:
function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
let finalObj = collection
.map(item => Object.entries(item))
.filter(el => String(el).includes(String(Object.values(source))))
.map(el => Object.fromEntries(el))
arr.push(finalObj);
console.log(arr);
// Only change code above this line
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }) // should return [{ first: "Tybalt", last: "Capulet" }]
whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 }) // should return [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]
whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }) // should return [{ "apple": 1, "bat": 2, "cookie": 2 }]
whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 }) // should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]
whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}) // should return []
uj5u.com熱心網友回復:
- 使用
Object#entries,從中獲取鍵值對串列source - 使用
Array#filter,迭代collection。在每次迭代中,使用Array#every,檢查上面的所有條目是否sourceEntries與當前物件匹配
function whatIsInAName(collection, source) {
const sourceEntries = Object.entries(source);
return collection.filter(e =>
sourceEntries.every(([key, value]) => e[key] === value)
);
}
console.log(
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })
);
console.log(
whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })
);
console.log(
whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })
);
console.log(
whatIsInAName([{ "a": 1, "b": 2, "c": 3 }], { "a": 1, "b": 9999, "c": 3 })
);
要分析您的代碼,讓我們先添加日志:
function whatIsInAName(collection, source) {
const arr = [];
let finalObj = collection
.map(item => {
const entries = Object.entries(item);
console.log(1, entries);
return entries;
})
.filter(el => {
console.log(2, String(el), String(Object.values(source)));
return String(el).includes(String(Object.values(source)))
})
.map(el => {
const obj = Object.fromEntries(el);
console.log(3, obj);
return obj;
});
console.log(4, finalObj);
arr.push(finalObj);
console.log(5, arr);
return arr;
}
whatIsInAName([{ "apple": 1, "bat": 2 }], { "apple": 1, "bat": 2 });
總之,您的方法是遍歷collection,將每個專案轉換為條目串列。然后,通過檢查專案的條目(字串化)是否包含源(字串化)的值來過濾這些條目。之后,將通過過濾的條目轉換回物件,從而生成物件陣列。將此陣列添加到初始陣列并回傳后者。
問題:
source例如,當有多個屬性時它會停止作業,例如,您將檢查“apple,1,bat,2”是否包含“1,2”。即使您source在這里檢查條目,您仍然會假設屬性的順序。總之,對物件進行字串化不是正確的方法,這就是為什么最好檢查每個鍵是否source與其在給定專案中的值匹配的原因。- 第三個
Array#map回傳一個陣列,然后將它(finalObj)添加到另一個陣列arr。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406984.html
標籤:
上一篇:忽略非必填欄位
