简体   繁体   English

如何遍历多个对象并将键值与字符串进行比较

[英]How to loop through multiple Objects and compare key values to string

I am trying to loop through multiple objects and compare each date_created key value to a string.我正在尝试遍历多个对象并将每个 date_created 键值与字符串进行比较。 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.这些对象来自一个数组,我可以将其映射并输出如下结果:一旦我遍历每个单独的对象,如果 date_created 键值等于我的字符串,我想将它们推送到一个数组中。

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", …}

I hope this helps.我希望这有帮助。 But dont forget Array.map is returns array.但不要忘记Array.map是返回数组。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM