1

hey i have this array

[
 0: "Migration, MD"
 1: "Lution, MD"
 2: "Mover, MD"
 3: "Dee"
 4: "Prov10A"
]

how to get the value which has the word MD in it like which has MD it should give me something like this

[
 0: "Migration, MD"
 1: "Lution, MD"
 2: "Mover, MD"
]

how can i do that with lodash? thanks

2
  • By word MD do you mean that you only want to match items that contain the string MD separated by word boundaries (spaces, punctuation marks, etc.) ? Commented May 13, 2019 at 5:16
  • which contains string MD just Commented May 13, 2019 at 11:14

4 Answers 4

1

Use filter and includes:

const arr = ["Migration, MD", "Lution, MD", "Mover, MD", "Dee", "Prov10A"];
const res = arr.filter(e => e.includes("MD"));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

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

1 Comment

This is good although if there was a value that contained "MD" not as a separate title but as part of the name it would also treat is as MD.
0

You can use filter, toLowerCase & includes. filter will return a new array, toLowerCase will convert the case of the element so both md ,MD will be considered.

let k = ["Migration", "MD", "Lution, MD", "Mover, MD", "Dee", "Prov10A"].filter(function(item) {

  return item.toLowerCase().includes('md')
});
console.log(k)

Comments

0

you can try the filter function

    const  arr = [
     "Migration, MD", "Lution, MD" , "Mover, MD", "Dee", "Prov10A"
    ]

    const out  = arr.filter(e => e.includes('MD'))
    console.log(out)

1 Comment

same as first comment it solved my problem but thanks anyway :)
0

If you want to find only one word with "MD" or only the first one, you can use java script find method, and if you want an array of words having "MD" then you can use filter method.

 const  arr = [
     "Migration, MD", "Lution, MD" , "Mover, MD", "Dee", "Prov10A"
    ]
    const result = arr.find(word=>word.includes("MD"));
    console.log(result);
// to get array of words with MD simply change .find as .filter in above code.

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.