我在下面的数组对象中有数据,想转换另一种数组对象格式
Posted
技术标签:
【中文标题】我在下面的数组对象中有数据,想转换另一种数组对象格式【英文标题】:I have data in below array object and want to convert another array object format 【发布时间】:2021-08-12 14:00:23 【问题描述】:我的 API 响应 -
let data = [
date: '2021-04-27',
formatted_date: 'Apr 27',
location: [
date: '2021-04-27',
formatted_date: 'Apr 27',
country: 'India',
total_views: 6,
formatted_views: '6',
total_watch_duration: 115,
formatted_watch_duration: '0h 1m',
,
date: '2021-04-27',
formatted_date: 'Apr 27',
country: 'USA',
total_views: 16,
formatted_views: '16',
total_watch_duration: 1195,
formatted_watch_duration: '0h 1m',
,
date: '2021-04-27',
formatted_date: 'Apr 27',
country: 'Canada',
total_views: 16,
formatted_views: '10',
total_watch_duration: 1195,
formatted_watch_duration: '0h 1m',
,
],
,
date: '2021-04-28',
formatted_date: 'Apr 28',
location: [
date: '2021-04-28',
formatted_date: 'Apr 28',
country: 'India',
total_views: 1,
formatted_views: '1',
total_watch_duration: 21,
formatted_watch_duration: '0h 0m',
,
date: '2021-04-28',
formatted_date: 'Apr 27',
country: 'UK',
total_views: 16,
formatted_views: '16',
total_watch_duration: 1195,
formatted_watch_duration: '0h 1m',
,
date: '2021-04-28',
formatted_date: 'Apr 27',
country: 'China',
total_views: 16,
formatted_views: '10',
total_watch_duration: 1195,
formatted_watch_duration: '0h 1m',
,
],
,
date: '2021-04-29',
formatted_date: 'Apr 29',
location: [
date: '2021-04-29',
formatted_date: 'Apr 29',
country: 'India',
total_views: 2,
formatted_views: '2',
total_watch_duration: 37,
formatted_watch_duration: '0h 0m',
,
],
,
date: '2021-04-30',
formatted_date: 'Apr 30',
location: [
date: '2021-04-30',
formatted_date: 'Apr 30',
country: 'India',
total_views: 1,
formatted_views: '1',
total_watch_duration: 0,
formatted_watch_duration: '0',
,
date: '2021-04-30',
formatted_date: 'Apr 27',
country: 'USA',
total_views: 16,
formatted_views: '16',
total_watch_duration: 1195,
formatted_watch_duration: '0h 1m',
,
date: '2021-04-30',
formatted_date: 'Apr 27',
country: 'Canada',
total_views: 16,
formatted_views: '10',
total_watch_duration: 1195,
formatted_watch_duration: '0h 1m',
,
],
,
date: '2021-05-01',
formatted_date: 'May 01',
location: [],
,
date: '2021-05-02',
formatted_date: 'May 02',
location: [],
,
date: '2021-05-03',
formatted_date: 'May 03',
location: [],
,
date: '2021-05-04',
formatted_date: 'May 04',
location: [
date: '2021-05-04',
formatted_date: 'May 04',
country: 'India',
total_views: 4,
formatted_views: '4',
total_watch_duration: 584,
formatted_watch_duration: '0h 9m',
,
],
,
date: '2021-05-05',
formatted_date: 'May 05',
location: [],
,
];
如果位置包含空,我想在下面的格式中考虑 0。假设数据的 total_views 值。
this.geographySeriesData = [
name: 'India',
data: [500, 555, 444, 777, 877, 9944, 750],
,
name: 'USA',
data: [10, 1000, 1200, 1000, 1200, 1000, 500],
];
【问题讨论】:
您是否已经尝试过?很高兴已经在这里直接向我们提供您的一些试验 【参考方案1】:您可以使用此代码进行转换。这不是最佳的,但非常简单易懂。您需要的数据存储在 finalResult 变量中。
const result = ;
const locations = data.map(el => el.location).flat();
locations.forEach(loc =>
result[loc.country] = [];
);
locations.forEach(loc =>
result[loc.country].push(loc.total_views);
);
const finalResult = Object.keys(result).map(key => ( name: key, data: result[key]));
【讨论】:
【参考方案2】:你可以像这样实现精确的格式
countryList = []
countryObject =
data.forEach((d) =>
d.location.forEach((loc) =>
if(loc.country in countryObject)
countryViewsList = countryObject[loc.country]
countryViewsList.push(loc.total_views)
countryObject[loc.country] = countryViewsList
else
countryObject = Object.assign(countryObject, [loc.country]: [loc.total_views])
)
)
Object.keys(countryObject).forEach((k)=>
countryList.push(
name: k,
data: countryObject[k]
)
)
countryList
正是您要求的格式。
【讨论】:
以上是关于我在下面的数组对象中有数据,想转换另一种数组对象格式的主要内容,如果未能解决你的问题,请参考以下文章