Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find string value in javascript array of arrays

I’m new to javascript and I’m currently working off a previous question that I’m struggling to understand, where in this one I’m checking for the matching number, but I’d like to check for the string value given. I don’t fully understand this below, any help would be appreciatted.

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

const check = 1;

console.log(arr.filter(({ 1: n }) => n === check))

but I’d like to check for a string value, example check = "posts", if possible would you mind explaining as well? Much appreciated

check related question here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can check reference for more info. However what I understood from your question is you are trying to achieve the following:

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

console.log(arr.filter((n) => n[0] === check));

Please also explore many other blogs for any functions you get confused in.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading