Exploring methods to access specific values from an array containing multiple values using Lodash in Angular 4

Hey, I have an array that looks like this:

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

I would like to extract the values that contain the word "MD" in them. In other words, I want a result like this:

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

Is there a way to achieve this using lodash? Thank you!

Answer №1

Implement filter along with includes:

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

Answer №2

To achieve this, you can utilize the combination of filter, toLowerCase, and includes. By using these methods, you can create a new array while ignoring the case sensitivity, so both md and MD will be recognized.

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

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

Answer №3

One approach you could take is by using the filter function in JavaScript


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

    const filteredCities = cities.filter(city => city.includes('MD'))
    console.log(filteredCities)

Answer №4

If you're looking to locate a single word containing "MD" or just the first one, you can utilize the JavaScript find method. However, if you require an array of words that include "MD," then the filter method is your best bet.

 const  arr = [
     "Migration, MD", "Lution, MD" , "Mover, MD", "Dee", "Prov10A"
    ]
    const result = arr.find(word=>word.includes("MD"));
    console.log(result);
// To obtain an array of words with 'MD', all you need to do is change .find to .filter in the above code snippet.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS Setting Default Values in HTML Pages

My goal is to set the default value for a dropdown in an Angular HTML Page. The page uses tabs, and I want the default value to load when a specific tab is clicked. <div class="col-md-9" ng-cloak ng-controller="ServiceTypeController"> <md-conten ...

Setting the parent's height to match one of its children

I'm struggling to align the height of the pink '#images-wrap' with the main image. When there are too many small rollover images on the right, it causes the pink div to extend beyond the main image's height. If I could make them match i ...

Establish an HttpOnly cookie using JavaScript

Is it possible to convert a non-HttpOnly cookie into an HttpOnly cookie using JavaScript? ...

"Trouble with props: List items not showing up after refreshing the page

I am facing an issue with my "Event Selector" component where it is not displaying the list items as expected. The component is supposed to create a button for each item in the 'lists' passed via props. Strangely, the items do not show up upon re ...

What are the steps for transitioning an Angular application from MonoRepo to PolyRepo?

Having created three separate Angular applications with individual workspaces, projects, and repositories, I am able to share modules among them using @angular-architects/module-federation. However, I am facing challenges when it comes to sharing component ...

What could be causing the lack of change in direction for the initial function call?

There appears to be an issue with image(0) and image(1) in this array that I can't quite understand. The console output shows: i=5; div class="five" id="slides" i=0; div class="one" id="slides" i=1; div class= ...

Is there a standard event triggered upon the closing of a browser tab or window?

Does React have a built-in event that is triggered when a browser tab or window is closed? If it does, does it have support across different browsers? ...

I'm curious about the distinction between React's one-way data binding and Angular's two-way data binding. Can someone please clarify the key differences

My understanding of these concepts is a bit hazy. If I were to develop the same ToDo application using AngularJS and ReactJS, what exactly distinguishes React ToDo's use of one-way data binding from AngularJS's two-way data binding? From what I ...

ng-select issue: list not refreshing

I am encountering an issue with the method below that updates the modules array. Even though the console displays the result correctly, the ng-select does not update the list accordingly. I attempted to use this.modules=[...elements], but it did not work ...

What steps do I need to take to create a custom image for a website?

I'm looking for a way to create a website image using just code, without the need for an actual image file or image tag. Is there a method to do this? Would I use CSS, Javascript, or HTML5 for this task? If using JavaScript to dynamically generate the ...

Guide on handling 404 page redirection within an axios service in Next.js

Currently, I am utilizing axios to handle my API calls. One thing that I want to achieve is checking the status of the response received from the api and potentially redirecting to a 404 page based on that. const api = axios.create({ headers: { commo ...

Dealing with Unexpected Timeout Errors and Memory Leaks in Express/Typescript Using Jest, Supertest, and TypeORM

Currently, I am in the process of writing unit tests for an express API. Each test suite initiates a fresh instance of an express server before running the tests. Individually, each test suite runs smoothly without any problems. However, when executed tog ...

how to save an array within an ArrayList in Java

I have been attempting to save an array within an ArrayList using a method like the one shown below: static ArrayList<double []> Shapes; public static void Test(){ double [] Test = new double [2]; Test[0] = 1; Test[1] = 2; Shapes.add ...

Ensure that the view is only updated once the JSON has been completely received from the AJAX call in AngularJS

Just starting out with angularJS and still in the learning phase. I'm currently working on creating a navbar for the header that will fetch data from an ajax call. The issue I'm facing is that it displays {{obj.pageData}} until the data is fully ...

Direction of Agm: Display the panel within a separate component

I have a unique setup where I have divided my page into two components, with one taking up 70% of the space and the other occupying 30%. The first component contains a map while the second one is meant to display information on how to reach a destination ( ...

FabricJS Canvas with a React DropDown Feature

While I have successfully created a TextBox on FabricJS Canvas, creating a Dropdown component has proven to be a challenge. The fabric.Textbox method allows for easy creation of text boxes, but no such built-in method exists for dropdowns in FabricJS. If y ...

The Link component in the router dom should only be active when validation passes successfully

I am looking for a way to prevent the Link component in react-router-dom from functioning until all validations are successfully completed. Removing the link allows the validation to work as intended. I have come across something related to ifValidate, bu ...

Toggling the visibility of a div using JavaScript

When I click the button, I want to show and then hide a div. However, it doesn't work on the first click - only on the second click. How can I make it work on the first click? HTML <p>Click the button</p> <button onclick="myFu ...

Error in retrieving JSONP request

My goal is to send a request to the following URL: https://en.wikipedia.org/w/api.php?action=opensearch&search=apple&limit=5&namespace=0&format=json I want to use JSONP for this. The function I intend to utilize for making this request i ...

Encountering the error "Object potentially undefined" while attempting to activate Cloud Function during document creation

My goal is to automatically create a new authenticated user whenever a new document is added to the "users" collection. The necessary user details will be extracted from the fields "email" and "phone" in the new document. The challenge I am facing is that ...