我是 javascript 新手,我目前正在解決上一個我很難理解的問題,在這個問題中,我正在檢查匹配的數字,但我想檢查給定的字串值。我不完全理解下面的內容,任何幫助都會受到贊賞。
const arr = [["posts",{text: 'hello'}], ["likes", {text: 'liked'}],["comments", {text: 'commented'}]]
const check = 1;
console.log(arr.filter(({ 1: n }) => n === check))
但我想檢查一個字串值,例如check = "posts",如果可能的話,你介意解釋一下嗎?非常感激
在此處查看相關問題
uj5u.com熱心網友回復:
您可以查看參考以獲取更多資訊。但是,我從您的問題中了解到您正在努力實作以下目標:
const arr = [
["posts", { text: "hello" }],
["likes", { text: "liked" }],
["comments", { text: "commented" }],
];
const check = "posts";
console.log(arr.filter((n) => n[0] === check));
還請瀏覽許多其他博客,了解您對任何功能感到困惑。
uj5u.com熱心網友回復:
我不知道我是否完全理解你的問題,但這就是你要找的嗎?您只需要選擇每個陣列中的第一個元素進行比較。
const arr = [["posts",{text: 'hello'}], ["likes", {text: 'liked'}],["comments", {text: 'commented'}]]
console.log(arr.filter(arr => arr[0] === "posts"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/349427.html
標籤:javascript
