0

Hi below is array of strings

const arr = [
    "list/1/item/1/",
    "some-domain/2/item/2/",
    "item/3/",
    "some-domain/4/item/5/subitem/1/",
]

i have to filter those strings that start with string "item/3/" or ends with string "item/3/"

so from above array expected filtered array is like below,

const filtered_arr = [
    "list/1/item/1/",
    "some-domain/2/item/2/",
    "item/3/",
]

from the filtered array i want to get the number after "/item/". so from above filtered array the expected output is

const arr_ids = ["1", "2", "3"]

what i have tried,

i have used below to match those strings that start or end with /item/3/

const filtered_arr = arr.map(str) => {
    return str.match(/item\/[0-9](\/)?$/g);
}

this gives the filtered_arr

const filtered_arr = [
    "list/1/item/1/",
    "some-domain/2/item/2/",
    "item/3/",
]

but how do i map through each array item and get the number after string "/item/".

could someone help me with this. thanks.

5
  • "i have used below to match those strings that start or end with /item/3" but you are not checking 3 explicitly? Commented Feb 6, 2022 at 11:54
  • 1
    Your expected filtered array doesn't make sense. How is item/3/ in that list? That clearly starts with item/3/, which you explicitly ruled out. Commented Feb 6, 2022 at 11:55
  • the number after item/ can be anything. like 3, 1 or any number Commented Feb 6, 2022 at 11:55
  • The question states i have to filter those strings that start with string "item/3/" or ends with string "item/3" and we find element item/3 which is not even present in the original array (the original array's element is item/3/). Please note: item/3 is NOT item/3/. These are different strings. Commented Feb 6, 2022 at 12:00
  • sorry for the typo i have edited the array and the expected output Commented Feb 6, 2022 at 12:00

2 Answers 2

1

Use filter to filter paths either starting or ending in item/\d+/. Then use map to extract the item number from each matching path.

const arr = [
    "list/1/item/1/",
    "some-domain/2/item/2/",
    "item/3/",
    "some-domain/4/item/5/subitem/1",
];
var output = arr.filter(x => x.match(/^item\/\d+|\bitem\/\d+\/$/))
                .map(x => x.match(/(?<=^item\/)\d+|(?<=\bitem\/)\d+(?=\/$)/)[0]);
console.log(output);

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

4 Comments

thanks but says object could be possibly null on map function
Your data has some problem. My code is working with the sample data you actually provided.
yes there was a problem with the way i filter. now it works.
but this seems to also match the strings where the string item/3/ is followed by other string. for example it shouldnt match strings like "item/3/subitem/1" . meaning string item/3/ shouldnot be matched if its followed by another string. it can match only if its starts with item/3/ and has no following string or ends with item/3/. thanks
1

This may help:

const filtered_arr = arr.map(str) => {
    const match = str.match(/\/item\/(\d)/);
    return(match[1]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.