0

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

3
  • 1
    please add what you like to compare. you hane more than one string in the structure ... Commented Nov 5, 2021 at 20:09
  • @NinaScholz only want to compare the strings in the arrays "posts"/"comments"/"likes", I don't want to search through the objects but only the first string in the array Commented Nov 5, 2021 at 20:11
  • @NinaScholz I've added the link to a similar question I've mentioned Commented Nov 5, 2021 at 20:13

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Anytime :) . i would highly encourage you to explore the functions when you encounter these small problems. They will give you better understanding.
I agree, I'll take that into consideration:)
1

I don't know if I understand your question fully, but is this what you're looking for? You simply need to choose first element in each array to compare.

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

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

2 Comments

Exactly what i understood from the OP's question.
Thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.