Skip to main content
added 64 characters in body
Source Link
Robert Strauch
  • 13.1k
  • 33
  • 148
  • 238

I have an array of strings ["samantha", "mike", "john", "sammy", "carla"] and an input value of s.

const input = "s"; let names = ["samantha", "mike", "john", "sammy", "carla"]; let filteredNames = names.filter(name => name.startsWith(input)); 

This gives me "samantha" and "sammy" as a result.

How can I extract the string that all elements of filteredNames start with, in this case? So basically: How can I find out that sam is the matching string for this array?

I have an array of strings ["samantha", "mike", "john", "sammy", "carla"] and an input value of s.

const input = "s"; let names = ["samantha", "mike", "john", "sammy", "carla"]; let filteredNames = names.filter(name => name.startsWith(input)); 

This gives me "samantha" and "sammy" as a result.

How can I extract the string that all elements of filteredNames start with, in this case sam?

I have an array of strings ["samantha", "mike", "john", "sammy", "carla"] and an input value of s.

const input = "s"; let names = ["samantha", "mike", "john", "sammy", "carla"]; let filteredNames = names.filter(name => name.startsWith(input)); 

This gives me "samantha" and "sammy" as a result.

How can I extract the string that all elements of filteredNames start with? So basically: How can I find out that sam is the matching string for this array?

Source Link
Robert Strauch
  • 13.1k
  • 33
  • 148
  • 238

Find the string that all array elements begin with in Javascript

I have an array of strings ["samantha", "mike", "john", "sammy", "carla"] and an input value of s.

const input = "s"; let names = ["samantha", "mike", "john", "sammy", "carla"]; let filteredNames = names.filter(name => name.startsWith(input)); 

This gives me "samantha" and "sammy" as a result.

How can I extract the string that all elements of filteredNames start with, in this case sam?