Use lodash to filter out objects from an array that have a specific property

I'm dealing with a large array of objects and I need to retrieve the ones that have the property def defined, regardless of its value. Any help would be greatly appreciated.

Answer №1

To determine if a property exists, you can utilize the hasOwnProperty method and then apply Array.prototype.filter to extract specific items.

objArray = [ { def: 1, bar: 2}, { foo: 3, bar: 4}, { def: 5, bar: 6} ];

var result = objArray.filter(item => item.hasOwnProperty('def'));
console.log(result);

For compatibility with ES5:

objArray = [{
  def: 1,
  bar: 2
}, {
  foo: 3,
  bar: 4
}, {
  def: 5,
  bar: 6
}];

var result = objArray.filter(function(item) {
  return item.hasOwnProperty('def')
});
console.log(result);

Answer №2

Although lodash does not have a built-in function for this specific task, you can achieve the desired result by using the following code snippet:

`let filteredArray = [];
    _.forEach(inputArray, function(element){
      if(!_.isEmpty(element.definition)){
        filteredArray.push(element);
      }
    };

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

How can you call a function in ExpressJS that is located in a different file?

I am having trouble with my UserController and database configuration file db.js. I am trying to make a function in the UserController access a function in db.js. In my UserController, I have included var db = require('../config/db'); and within ...

Switching Between Background Images in Angular

Looking to change the background-image of an element upon clicking it. Currently, this is what's in place: <div class="item-center text-center" toggle-class="active cable"> <div class="quiz-background"></div> <p class="size ...

transferring data between pages without the need for a form tag

<a href='new.php?logi_jo=$_POST[jo]'>submit</a> <input type='text' style='width:50px' name='logi_jo'> Is there a method to extract the value of logi_jo and transfer it to another webpage without u ...

Exploring the complete meaning of React controlled and uncontrolled components

I have been using React for several months and I thought I understood the distinction between controlled and uncontrolled components, thanks to sources like this: Controlled form inputs Uncontrolled components However, my understanding was thrown into ...

Properly Formatting Objects in a JSON Array for Postman Testing

I am facing a challenge in Postman where I need to transfer the results of one request into another. The structure of the first request is as shown below: [ { "lfidField": "11111", "featureField": "Logging& ...

Tips for displaying a message in the model body when a bootstrap model does not have any data in jQuery

Having trouble displaying a text message in the Bootstrap modal body when there is no data available in the model. I have multiple cards in the model, and if I click on the skip or done buttons, each card will close. However, if there is only one card in t ...

The presentation of the Bootstrap carousel is not showing up as intended

I am currently utilizing Bootstrap 3.xx for website development and encountering difficulties in getting a carousel to function correctly. Website version - www.solo-fusion.com/test/index.htm <div id="Carousel" class="carousel slide col-lg-8 col-offse ...

Over time, a buildup of code can gradually impair the speed of the

Currently, I am in the process of working on a project for my Web Programming course at University. The homepage is almost complete, but during testing, I noticed that each time the scroll event is triggered, there is a significant slowdown. This particul ...

Exploring the integration of polyfills and the jQuery package in Next.js

Encountering a problem related to the Next.js array-flat polyfill. It is essential for me to have compatibility with older versions of Chrome, specifically in the range from v60 to v68. The project I am currently working on utilizes a video player library ...

Methods for determining the disparity in days between two dates and the ratio of days yet to come

Within my React project, I am in need of a functionality that calculates the number of days remaining between two specific dates. Essentially, I want to determine how many days are left from today until the expiration date, and then calculate the percentag ...

Refresh the image source using the AJAX success callback

Previously, I was updating Label values within the AJAX success function as shown below. Now, I am looking for a way to use the same method to change or update the "src" attribute of an <img id="myimage" src=""/> $.ajax({ url: 'clmcontrol_l ...

Value is extracted from the getDisplayedRowCount() function in the AGrig React and placed into

By utilizing the following code, I have been able to effectively console.log the value: console.log(this.gridApi.getDisplayedRowCount()); This snippet is placed inside the onGridReady function of my component. I am interested in retrieving this value an ...

Background image scrolling within defined limits

My webpage features a large background image that I want to have scroll down with the page. However, when the bottom of the image is reached, I don't want it to continue scrolling into white space. How can I achieve this effect? Furthermore, I would ...

Updating the content of an SVG file in real-time

I have an SVG file saved externally that includes the following code: <g inkscape:groupmode="layer" id="layer9" inkscape:label="score" style="display:inline"> <text xml:space ...

Retrieve an XML document using AJAX and then convert it to JSON format without needing a server

Seeking assistance with a code snippet I'm currently working on. Unfortunately, my knowledge of AJAX and jQuery is limited. Here's the issue: I am trying to retrieve XML data from an external XML file (text.xml) and convert it into a JSON object ...

Expanding the size of a buffer array dynamically in JavaScript while adding items

Within the source table are several records stored. Upon clicking each record in the source table, the AddSelectedItem(sValue) function is triggered, adding those specific records to the destination table. The desired outcome is for the array to dynamica ...

Delete the current code within the specified div element

Objective: The goal is to ensure that any HTML syntax code or data inside the specified <div id="feedEntries"></div> element is removed, leaving it empty. Issue: What specific syntax is required to remove all content within <div id="fe ...

Using Firebase to connect and disconnect from state in React with Re-base

I'm encountering some issues with React and Firebase while using the re-base module. When attempting to retrieve content from Firebase across different components in my app, I run into a problem. On one component/"page," which is home, I have the abi ...

Converting binary data to a datetime object with AngularJS

Need help converting a date format to the normal date format. For example, from 12-07-2017. Currently, my output looks like this: /Date(1508896907290) I am working with AngularJS. {{list.DateStamp}} Any suggestions on how to convert binary format to ...

Prevent scrolling on browser resize event

I am working on a basic script that adds a fixed class to a specific div (.filter-target) when the user scrolls beyond a certain point on the page. However, I am wondering how I can prevent the scroll event from triggering if the user resizes their brows ...