从对象中抓取字段并将其返回到数组中 [英] Grabbing fields from object and returning it in an array

查看:79
本文介绍了从对象中抓取字段并将其返回到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个链接对象

{scope: "T4251", data: Array(500)}
{scope: "T7910", data: Array(500)}
{scope: "T213910", data: Array(500)}

,数据对象包含500个这些字段(范围链接到下面)

and the data object consists of 500 of these fields (scope is linked to below)

{@id: "eddnsf", dateTime: "2018-12-12T07:30:00Z", value: -0.045}
{@id: "sfdkjk", dateTime: "2018-12-12T04:30:00Z", value: -0.345}
{@id: "dfjewf", dateTime: "2018-11-12T02:00:00Z", value: -0.445}
{@id: "dfjewf", dateTime: "2018-10-12T04:40:00Z", value: -0.045}
{@id: "dfjewf", dateTime: "2018-10-12T06:00:00Z", value: -0.145}
{@id: "dfjewf", dateTime: "2018-10-12T02:00:00Z", value: -0.045}

如何通过首先获取一个唯一的范围对象来迭代这个多对象,然后在每个日期(必须是唯一的)对象中抓取dateTime,包含在范围的数组对象?

How do I iterate this multi-object by first getting a unique "scope" object then grabbing the dateTime only once per date (must be unique) objects from the data array contained in the scope's array object?

我想要的抓住要放入单个数组的唯一dateTime

I want the unique dateTime grabbed to be put into a single array

数组应该导致:

[unique dateTime, unique dateTime, unique dateTime] 


推荐答案

你可以用两个功能来做。
首先收集dateTimes

You could do it with two functions. First collects the dateTimes

const extractField= (data, field) => data.map( obj => obj[field])

第二次过滤双打

const filterDoubles = array => array.reduce( (result, item) => result.indexOf(item) > -1 ? result : result.concat(item),[])

然后:

filterDoubles(extractField(yourObject, 'dateTime'));

这篇关于从对象中抓取字段并将其返回到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆