繁体   English   中英

如何在javascript数组中查找字符串值

[英]How to find string value in javascript array of arrays

我是 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" ,如果可能的话,你介意解释一下吗? 非常感激

在此处查看相关问题

您可以查看参考以获取更多信息。 但是,我从您的问题中了解到您正在努力实现以下目标:

 const arr = [ ["posts", { text: "hello" }], ["likes", { text: "liked" }], ["comments", { text: "commented" }], ]; const check = "posts"; console.log(arr.filter((n) => n[0] === check));

还请浏览许多其他博客,了解您对任何功能感到困惑。

我不知道我是否完全理解你的问题,但这就是你要找的吗? 您只需要选择每个数组中的第一个元素进行比较。

const arr = [["posts",{text: 'hello'}], ["likes", {text: 'liked'}],["comments", {text: 'commented'}]]

console.log(arr.filter(arr => arr[0] === "posts"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM