The _.findWhere function fails to work properly if the object contains a value represented as an array

When I try to use _.findWhere to search for an object with a value as an array, it always returns undefined.

var nameArray = [{name:'firstName',value : ['Amruta','Swapnil']},{name:'LastName',value : ['Pawar','shaikh']}]

var element = _.findWhere(nameArray,{value:'Amruta'});

console.log("element is " + element);

Instead of getting the actual value, I am receiving undefined as the output.

Answer №1

findWhere is a method that can be used to locate items based on their specific property value. In this scenario, you are attempting to find an item with a value property that contains a particular text. To achieve this, you can pass a predicate function as the second argument to the find method:

var nameArray = [{name:'firstName',value : ['Amruta','Swapnil']},{name:'LastName',value : ['Pawar','shaikh']}]

var element = _.find(nameArray,function(x){
    return x.value.includes("Amruta")
});

console.log(element);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

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

Make sure to include the "active" class in the pager once the slider begins

After coming across this fiddle, I noticed that it closely resembles the task I am working on. However, there is one issue - currently, only the first item has an active class when the slider autostarts. The next or previous items do not receive the acti ...

Having trouble releasing the package on npm platform

I'm facing difficulties in publishing a public package to npm. Despite creating an organization, I'm unable to figure out how to add a new package. Any assistance on this matter would be greatly appreciated. https://i.sstatic.net/RMKU9.png ...

What is the best way to effectively carry out a partial update using ReactJS?

During a recent job interview, the recruiter discussed the importance of handling partial updates and managing application size. As an example, he presented a Single Page Application (SPA) with a size of 8MB, which he deemed less than ideal. He emphasize ...

Make sure to wait for the current operation to finish before moving onto the next one

In the primeData function within the datacontext, four queries are made to a back-end Web API service: function primeData(forceRefresh) { return (getLookups(forceRefresh) // this needs to complete before moving on .then(success)) ...

Unable to connect state to props when utilizing combineReducers

I'm currently working on integrating my state with a specific component using Redux. To achieve this, I have implemented two reducers and utilized combineReducers to merge them into a rootReducer before initializing my store. const rootReducer = comb ...

Understanding how objects are created using the reduce method

I'm curious about how the reduce function can transform an array into an object as shown in this example, particularly with the line: p[c[0]] = c[1]; Is this an unconventional syntax for creating key-value pairs that I haven't encountered before ...

Issue with Vue v-for not updating data model variable

I am attempting to render a page with dynamic properties using the following code: <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="root"> <div v-for="current in 2&quo ...

Guidelines for allocating memory for a dense array in Javascript

When utilizing the new Array(size) constructor, it appears that if the value of size is not constant, certain browsers (such as Chrome) may create a sparse array, leading to slower access times compared to using the default constructor. You can see this pe ...

Creating these three functions directly in the Parent Component instead of duplicating code in the child component - ReactJS

I am new to React and currently working on a dashboard page that includes a React Table. The page features a customize button that opens a popup with checkboxes to show/hide columns in the table. By default, all checkboxes are checked but unchecking a colu ...

What is the proper way to invoke a child method after converting an object from a parent class to a child class?

When we have a subclass B that overrides a method from its superclass A in TypeScript, why does calling the method on an instance of A result in the parent class's implementation being called? In TypeScript, consider a class called Drug with properti ...

Combining an array with nested arrays

I have a complex array structure that I need to simplify [ { "entity": { "id": "2387232d-5fc4-4344-b71b-42f70ff16040", "pid": "bdba6327-4055-42db-8f2e-34c595a6f79f", "name&q ...

What is the best way to handle resolving a promise nested within another promise and retrieving the result within a controller?

I have a situation where I need to modify the result of a promise returned by a function in one service and make this altered value accessible to my controllers from another service. angular.module('test').service("service1", function($q) { ...

Partially obscured sprites peeking through

I'm trying to replicate a specific feature I saw in an app where a sprite remains partially visible even when it is behind a foreground mesh. Does anyone have any tips on how to achieve this effect using ThreeJS? Thank you! https://i.sstatic.net/vbrc ...

Manipulate CSS Properties with Javascript Based on Dropdown Selection

I am currently working on implementing a feature that involves changing the CSS property visibility: of an <input> element using a JavaScript function triggered by user selection in a <select> dropdown. Here's what I have so far in my cod ...

Having trouble getting autocomplete to work with JQuery UI?

Currently facing issues in implementing the Amazon and Wikipedia Autocomplete API. It seems that a different autocomplete service needs to be used based on the search parameter. Unfortunately, neither of the services work when adding "?search=5" for Wikipe ...

Axios is not properly sending all form data to MongoDB when used with React and Express

While working on a project involving data transmission from an HTML form element in React to MongoDB using Express.js, I encountered an issue where only the last input value from the Form element was being sent. Surprisingly, the console displayed the last ...

What is the best way to set up a .find() method that accepts an array of values and outputs an Object containing key-value pairs?

Is there a more efficient method to fetch multiple key-value pairs in an array of objects from MongoDB? I aim to create a function that takes an array of values to search for (e.g. an _id) and returns an Object with key-value pairs where the key is the or ...

Storing dynamic field data in React JS: Tips and Tricks

I need help figuring out how to save dynamic field values in a specific format to the database. Here is an example of the format I am looking for: "items": [ { "item_id" : 6, "description" : "", ...

Utilize a React Switch Toggle feature to mark items as completed or not on your to-do list

Currently, I am utilizing the Switch Material UI Component to filter tasks in my list between completed and not completed statuses. You can view the demonstration on codesandbox The issue I'm facing is that once I toggle a task as completed, I am un ...

AngularJS Cascading Dropdowns for Enhanced User Experience

There is a merchant with multiple branches. When I select a merchant, I want another dropdown list to display the data from merchant.branches. The following code does not seem to be fixing the issue: <label>Merchant:</label> <select ng-if= ...