【问题标题】:How can i get all values of a particular key from object array without loop in angular如何在没有角度循环的情况下从对象数组中获取特定键的所有值
【发布时间】:2022-01-02 15:50:21
【问题描述】:
[{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
]

这里我只想键入键所有值

这样 请帮我这样做,提前谢谢大家。

【问题讨论】:

  • 如果不循环遍历数组,您将无法做到这一点。但是,如果您正在寻找一些特定的用例,请同时添加您的用例。
  • 使用Array#map:myArray.map(item => item.type)。请注意,数组仍在隐式循环中。
  • 你可以使用map方法。

标签: arrays json angular object


【解决方案1】:

最简单的方法是像这样使用Array.map 方法:

let array = [{"time":"2016-07-26 09:02:27","type":"aa"},
  {"time":"2016-04-21 20:35:07","type":"ae"},
  {"time":"2016-08-20 03:31:57","type":"ar"},
  {"time":"2017-01-19 22:58:06","type":"ae"},
  {"time":"2016-08-28 10:19:27","type":"ae"},
  {"time":"2016-12-06 10:36:22","type":"ar"},
  {"time":"2016-07-09 12:14:03","type":"ar"},
  {"time":"2016-10-25 05:05:37","type":"ae"},
  {"time":"2016-06-05 07:57:18","type":"ae"},
  {"time":"2016-10-08 22:03:03","type":"aa"},
  {"time":"2016-08-13 21:27:37","type":"ae"},
  {"time":"2016-04-09 07:36:16","type":"ar"},
  {"time":"2016-12-30 17:20:08","type":"aa"},
  {"time":"2016-03-11 17:31:46","type":"aa"},
  {"time":"2016-05-04 14:08:25","type":"ar"},
  {"time":"2016-11-29 05:21:02","type":"ar"},
  {"time":"2016-03-08 05:46:01","type":"ar"},
]

array = array.map(i => i.type);
// ["ae", "ar", ...]

array = array.map(i => {return { type: i.type }})
// [ {"type": "ae"}, {"type": "ar"}, ... ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2014-10-17
    • 2011-12-21
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多