Reputation: 367
I am trying to loop through multiple objects and compare each date_created key value to a string. These objects are coming from an array that I can map over and output the results as follows: Once I loop through each individual object, if the date_created key value is equal to my string, I would like to push those to an array.
I've provided examples on how I'm currently retrieving the objects, I would just like to know how to loop / iterate over each object and compare the key value to my string.
Data that is returned initially
(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 to render list of objects (Map over data that is returned initially to pull objects out of array)
If this is the wrong way of getting my objects initially, please suggest a more efficient way of doing this.
resData.map(result => {
console.log(result);
});
Array of Objects Map Result:
{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", …}
Upvotes: 0
Views: 342
Reputation: 179
I hope this helps. But dont forget Array.map
is returns array.
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);
Upvotes: 1