当前位置:  开发笔记 > 编程语言 > 正文

如何读取javascript数组并将其重新格式化为JSON对象?

如何解决《如何读取javascript数组并将其重新格式化为JSON对象?》经验,为你挑选了1个好方法。

我根据给定的参数从Google Analytics API获取数据.这是一个JavaScript数组对象.

[ [ '201801', '(Other)', '129' ],
  [ '201801', 'Direct', '2236' ],
  [ '201801', 'Email', '2' ],
  [ '201801', 'Organic Search', '6263' ],
  [ '201801', 'Referral', '185' ],
  [ '201801', 'Social', '669' ],
  [ '201802', '(Other)', '371' ],
  [ '201802', 'Direct', '2037' ],
  [ '201802', 'Email', '3' ],
  [ '201802', 'Organic Search', '5790' ],
  [ '201802', 'Referral', '162' ],
  [ '201802', 'Social', '515' ],
  [ '201803', '(Other)', '213' ],
  [ '201803', 'Direct', '2465' ],
  [ '201803', 'Organic Search', '8596' ],
  [ '201803', 'Referral', '238' ],
  [ '201803', 'Social', '356' ],
  [ '201804', '(Other)', '65' ],
  [ '201804', 'Direct', '1872' ],
  [ '201804', 'Email', '1' ],
  [ '201804', 'Organic Search', '9275' ],
  [ '201804', 'Referral', '170' ],
  [ '201804', 'Social', '307' ],
  [ '201805', '(Other)', '35' ],
  [ '201805', 'Direct', '2429' ],
  [ '201805', 'Email', '2' ],
  [ '201805', 'Organic Search', '8995' ],
  [ '201805', 'Referral', '234' ],
  [ '201805', 'Social', '341' ],
  [ '201806', 'Direct', '51' ],
  [ '201806', 'Organic Search', '282' ],
  [ '201806', 'Referral', '1' ],
[ '201806', 'Social', '3' ] ]

如果不清楚,请查看此GIST:https://gist.github.com/chanakaDe/3ad4e2a51c99386a2737b65a82f034b1

在这个数组中,你可以看到像这样的'201801'.这意味着YEAR是2018年,MONTH是1st.它同样继续.

通常在一个特定月份,我们得到6个值,如(其他),直接,电子邮件,有机搜索.转介和社交.在此数组中,相同的日期重复6次.

我想要做的是创建一个简单的JSON对象.这是我想要创建的格式,以便在我的AngularJS前端显示所有这些数据.

[
    {
        "date" : "201801",
        "(Other)" : "129",
        "Direct" : "2236",
        "Email" : "2",
        "OrganicSearch" : "6263",
        "Referral" : "185",
        "Social" : "669"
    },
    {
        "date" : "201802",
        "(Other)" : "371",
        "Direct" : "2037",
        "Email" : "3",
        "OrganicSearch" : "5790",
        "Referral" : "162",
        "Social" : "515"
    }
]

我想要一个像这样的JSON数组.我试图找到重复的值,然后尝试将它们合并到一个JSON对象,如果附近的元素相同.许多尝试都是这样的.但仍然没有好处.拜托,伙计们,此刻真的想要你的帮助.我不擅长数据分析和这些排序.请帮帮我.该项目使用Node.js 8.10.0完成.



1> Nina Scholz..:

您可以将日期作为哈希表的键,并收集所有键值对.稍后只获取对象数组的值.

var array = [['201801', '(Other)', '129'], ['201801', 'Direct', '2236'], ['201801', 'Email', '2'], ['201801', 'Organic Search', '6263'], ['201801', 'Referral', '185'], ['201801', 'Social', '669'], ['201802', '(Other)', '371'], ['201802', 'Direct', '2037'], ['201802', 'Email', '3'], ['201802', 'Organic Search', '5790'], ['201802', 'Referral', '162'], ['201802', 'Social', '515'], ['201803', '(Other)', '213'], ['201803', 'Direct', '2465'], ['201803', 'Organic Search', '8596'], ['201803', 'Referral', '238'], ['201803', 'Social', '356'], ['201804', '(Other)', '65'], ['201804', 'Direct', '1872'], ['201804', 'Email', '1'], ['201804', 'Organic Search', '9275'], ['201804', 'Referral', '170'], ['201804', 'Social', '307'], ['201805', '(Other)', '35'], ['201805', 'Direct', '2429'], ['201805', 'Email', '2'], ['201805', 'Organic Search', '8995'], ['201805', 'Referral', '234'], ['201805', 'Social', '341'], ['201806', 'Direct', '51'], ['201806', 'Organic Search', '282'], ['201806', 'Referral', '1'], ['201806', 'Social', '3']],
    result = Object.values(
        array.reduce((r, [date, key, value]) => {
            r[date] = r[date] || { date };
            r[date][key] = value;
            return r;
        }, {})
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有