Guide on iterating through multiple Objects while comparing key values with a string

My goal is to iterate through multiple objects, comparing the date_created key value of each object to a specific string. These objects are retrieved from an array that I can map over to display the results. Once I loop through each object, I want to push those with a date_created key value equal to my string into a new array.

I have included examples of how I currently fetch the objects, but I am seeking guidance on how to efficiently loop through and compare the key value to the specified string.

Initial Data Output

(14) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
  0: {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
  date_created: "2020-08-10 01:28:59", date_updated: "X", …}

  1: {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
  date_created: "2019-11-08 02:56:03", date_updated: "X", …}

  2: {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
  date_created: "2019-10-09 15:05:16", date_updated: "X", …}
)

Code for rendering list of objects (Mapping over initial data to extract objects from array)

If there is a more effective way to initially retrieve the objects, please advise.

resData.map(result => {
  console.log(result);
});

Result of Mapping Array of Objects:

{4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
date_created: "2020-08-10 01:28:59", date_updated: "X", …}

{4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
date_created: "2019-11-08 02:56:03", date_updated: "X", …}

{4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
date_created: "2019-10-09 15:05:16", date_updated: "X", …}

Answer №1

Hopefully this explanation is beneficial. It's important to keep in mind that Array.map returns an array.

Source

const wantedDate = "2020-08-11 01:28:59";
const newArr = [];

const arr = [
  {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
  date_created: "2020-08-10 01:28:59", date_updated: "X"},
  {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
  date_created: "2020-08-11 01:28:59", date_updated: "X"},
  {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: "X", 
  date_created: "2020-08-10 01:28:59", date_updated: "X"}
]

arr.map((values) => {
  if(values.date_created === wantedDate) {
    newArr.push(values);
  }
})

console.log(newArr);

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

Determine whether an object exists within another object and merge its value into the formatted object

When filling out a form, I have a formattedData object that holds a deep copy of the original data object. If a field in the form is changed, I must update the formatted data object with properties from the values object. To simulate this scenario, I crea ...

Creating a duplicate or copy of an element in jQuery using

My dilemma involves a div that consists of text/images and a custom jQuery plugin designed for those images. The plugin simply executes a fadeIn/fadeOut effect every 3 seconds using setInterval. This particular div acts as a "cache" div. When a user wants ...

Extracting <p> elements from a separate HTML file and cycling through them

I successfully created a website using only HTML, JavaScript, and jQuery without any server-side scripting or language. Within my website, I have a simple stream.html page that remains unstyled with no CSS formatting. <html> <head> </head& ...

Utilizing Selenium and Python to extract data from JavaScript tables via web scraping

I've come across numerous articles about Web Scraping, but I'm still struggling to figure out how to locate specific elements on a website. The site where I want to scrape the tables can be found here: My target tables are: "TB01," "TB02," "TB0 ...

Transmitting a pair of data points to PHP using ajax POST for querying the SQL database

I am attempting to send two values from a form to another PHP file using the ajax post method. One value is the current input box value, while the other is the value being typed into a different input box which functions as a search box. When I execute the ...

The function cannot be called on params.getValue()

I am facing an issue while trying to retrieve the value of a specific row's column ID. The goal is to change the URL by appending the retrieved ID when clicking on an icon. However, I encountered an error stating TypeError: params.getValue is not a fu ...

How to activate a single element within a React array

I am currently working on developing a comment system similar to the one found on YouTube. In my setup, when I click on modify, all comments turn into input fields, but only the selected input should be modified. How can I trigger only the element I clicke ...

Updating the DOM after making changes with Maquette involves calling the `renderMaquette

In a previous discussion, I expressed my desire to utilize Maquette as a foundational hyperscript language. Consequently, avoiding the use of maquette.projector is essential for me. However, despite successfully appending SVG objects created with Maquette ...

The issue with VueEditor in Nuxt.js is that it's throwing an error

When working on my Nuxt.js project, I integrated the vue2-editor package for writing articles with HTML. Everything functions properly when I enter text and submit it, however, upon reloading the page, I encounter an error stating "document is not defined" ...

Pass the $scope object from a controller to a modal controller

I am facing an issue with transferring the $scope variable from ctrlone to ctrltwo, which is a modal window on my page. When I open the modal in ctrlone, my code looks like this: var modalInstance = $modal.open({ templateUrl: 'Modal.html&ap ...

Dynamically access nested objects by utilizing an array of strings as a pathway

Struggling to find a solution for accessing nested object properties dynamically? The property path needs to be represented as an array of strings. For example, to retrieve the label, use ['type', 'label'] I'm at a roadblock wit ...

What is the best way to utilize Office365 Excel array formulas to find the minimum and maximum values for each individual?

Looking at the test data provided below, how can we design the array formula to achieve the expected outcome? =LET(item,G4,name,A4:A7,data,C4:E15, names, CHOOSECOLS(data,1), items, CHOOSECOLS(data,2), cnt, CHOOSECOLS(data,3), mn, MIN(I ...

Performing a bulk update using the array provided as the base for the update statement

In my database, there is a table called sched that I frequently need to update. To facilitate this process, I have created an HTML interface that displays the current values of the sched table for users to edit. Upon making changes, scripts are triggered ...

What is the best way to include basic static files and HTML together in a NodeJS environment?

I am facing an issue trying to serve an HTML file with its CSS and JS files in NodeJS using express.static(), but unfortunately, it is not working as expected. I have followed the steps shown in several tutorials, but for some reason, the output is not co ...

Vue.js error: Unable to locate requested resources

Recently, I've been encountering a curious issue when trying to access a specific component in my application. When I manually type the URL http://localhost:8080/detailed-support/start into the browser, the content loads without error. However, none o ...

Using jQuery to manipulate text

Can I modify this code to function within "{}" instead of using the .chord tags? (Jquery) //This code transposes chords in a text based on an array var match; var chords = ['C','C#','D','D#','E',&apo ...

Jest-Native encountered an error: "SyntaxError: Unable to use import statement outside of a module"

Trying to perform unit testing using https://github.com/callstack/react-native-testing-library along with https://github.com/testing-library/jest-native. I am able to test plain JavaScript files without any issues, but I am facing an error when testing com ...

What is the best way to handle the select event for a jQuery UI autocomplete when there are images involved?

Looking for help with creating an autocomplete feature with images on this jsfiddle. Despite trying to capture the event when a user selects an image, it doesn't seem to work properly: $("#input").autocomplete({ //source: tags, so ...

Tool for obfuscating client-side files using node.js

I'm in search of a tool similar to the one found at but in the form of a node.js module, allowing for obfuscation of client-side js files prior to transmission. The tool mentioned above performs various tasks, with its most crucial function being th ...

"Troubleshooting issues with Material Design components in AngularJS: Why is <md-select> not functioning correctly

I'm attempting to implement the <md-select> tag, but I can't seem to achieve the same result as shown here. This is the code I've written: <div layout="column" layout-align="center center" style="margin: 0px 10px 0px 5px;"> & ...