How to find strings within an array

Asked

Viewed 73 times

3

Good morning!

I have the following array:

const keys = ["idCard1", "idCard2", "idCard3", "oi", "idCard4"]

I need to save in another array only what starts with the word "idCard", would be possible?

User attempt:

let keys = [idCard1, idCard2, idCard3,  oi, idCard4]
let filtro = keys.filter(item => item.includes("idCard"));
console.log(filtro);

This attempt had been placed in the field of answers.

  • Function isBigEnough(value) { Return value >= 10; } var Filtered = [12, 5, 8, 130, 44]. filter(isBigEnough); // filtered is [12, 130, 44]

  • But I want to take everything that starts with the word "idCard", this function would serve?

  • This answers your question? Filter string array

  • Let names = ["Thiago Carvalho", "Renata Carvalho", "Alexandre Otoni", "Guilherme Otoni de Carvalho"]; Let filter = names.filter(item => item.includes("Carvalho")); console.log(filter);

  • Very quiet at this time, this here const keys = [idCard1, idCard2, idCard3, oi, idCard4] is not an array of strings, it is an array composed of the value of several variables that may or may not be strings.

  • I posted the code test, which didn’t work

  • 1

    I didn’t present a tentative, I don’t kick. I’m warning you that this is what you put in the question const keys = [idCard1, idCard2, idCard3, oi, idCard4] is not an array of strings is an array of variables.

  • 1

    An array of strings would look like this const keys = ["idCard1", "idCard2", "idCard3", "oi", "idCard4"]. So I ask you are sure that the first form presented is correct?

  • You are correct, my way of sending here was really wrong. My array is strings, but I brought it here as a variable. Still, I could find what I need?

  • Edit the question and leave it in the correct way and mark me in a comment after the issue that when coming back from lunch I give you an answer. That’s if someone hasn’t already answered it after editing.

  • Ready, Augusto. I have already edited. I would appreciate it if you could help me. Thanks in advance

Show 6 more comments

2 answers

3

Use the method Array.prototype.filter() to create a new array with all the elements that passed the given test in a function passed as parameter to filter().

To implement the test use the method String.prototype.startsWith() that determines whether a string starts with specified characters returning true or false.

const keys = ["idCard1", "idCard2", "idCard3", "oi", "idCard4"]

let result = keys.filter((e) => e.startsWith("idCard"));

console.log(result)

1

Another way to solve this problem is by using a loop structure for in conjunction with the method startsWith javascript.

Follow an example:

const chave = ['idCard1', 'idCard2', 'idCard3',  'oi', 'idCard4']
const result = []
for (let k = 0; k < chave.length;k++) {
    if  (chave[k].startsWith('idCard')) {
        result.push(chave[k])
    }
}
console.log(result)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.