开发者社区> 问答> 正文

关于 Javascript Json 数据提取并且转换

有原始JSON数据如下:

"basic_data": {
  "2016-03-24": {
    "view_total": 17,
    "share_total": 0,
    "like_total": 1,
    "post_total": 2,
    "favorite_total": 1,
    "comment_total": 1
  },
  "2016-03-25": {
    "view_total": 10,
    "share_total": 0,
    "like_total": 0,
    "post_total": 58,
    "favorite_total": 1,
    "comment_total": 0
  },
  "2016-03-26": {
    "view_total": 0,
    "share_total": 0,
    "like_total": 0,
    "post_total": 0,
    "favorite_total": 0,
    "comment_total": 0
  },
  "2016-03-27": {
    "view_total": 0,
    "share_total": 0,
    "like_total": 0,
    "post_total": 0,
    "favorite_total": 0,
    "comment_total": 0
  },
  "2016-03-28": {
    "view_total": 0,
    "share_total": 0,
    "like_total": 0,
    "post_total": 0,
    "favorite_total": 0,
    "comment_total": 0
  },
  "2016-03-29": {
    "view_total": 11,
    "share_total": 0,
    "like_total": 0,
    "post_total": 42,
    "favorite_total": 0,
    "comment_total": 0
  },
  "2016-03-30": {
    "view_total": 4,
    "share_total": 0,
    "like_total": 0,
    "post_total": 3,
    "favorite_total": 0,
    "comment_total": 0
  }
}

`如何使用原生或者lodash等工具提取如下数据
全选复制放进笔记{
view_total: [17, 10, 0, 0, 0, 11, 4]
}`

展开
收起
爵霸 2016-03-05 08:14:18 2340 0
1 条回答
写回答
取消 提交回答
  • lodash版:

    var data = _.reduce(basic_data, (result, value) => {
      result.view_total.push(value.view_total);
      return result;
    }, {
      view_total: []
    });

    js版:

    var data = {
      view_total: Object.keys(basic_data).map(v => {
        return basic_data[v].view_total;
      })
    };
    
    console.log(data);
    2019-07-17 18:52:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript异步编程 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载