为了账号安全,请及时绑定邮箱和手机立即绑定

使用脚本进行 JSON 操作

使用脚本进行 JSON 操作

慕桂英3389331 2022-09-23 16:16:47

我有包含数据的 json 文件。我想像这样打印输出:


jan - male 1 female 2

feb - male 1 female 1

march - male 2 female 2

我的 json 文件看起来像这样:


[

  {

    "id": "qvODYl5PRcN4bR3yotVh",

    "data": {

      "registration_time": "2020-04-30 21:11:21",

      "sex": "Female"

    }

  },

  {

    "id": "qvODYl7PucN4bR3yotVh",

    "data": {

      "registration_time": "2020-03-30 21:11:21",

      "sex": "Male"

    }

  }

]

这是完整的 json 文件 https://pastebin.com/Zmy8mNZN


这是我到目前为止的代码:


global.fetch = require("cross-fetch");


fetch("https://pastebin.com/raw/fvJkWEk5")

  .then(response => {

    return response.json();

  })

  .then(data => {

    data.forEach(users => {

      var subset = {

        registration_time: users["data"]["registration_time"],

        sex: users["data"]["sex"]

      };

      var sex = subset["sex"];

      var date = new Date(subset["registration_time"]);

      var m = date.getMonth();


      var months = [

        "Jan",

        "Feb",

        "March",

        "April",

        "May",

        "June",

        "July",

        "August",

        "Sept",

        "Oct",

        "Nov",

        "Dec"

      ];

      console.log(months[m] + "  " + sex);

    });

  })

  .catch(err => {

    console.error(err);

  });

这似乎是一项简单的任务,但我发现很难实现。


查看完整描述

3 回答

?
元芳怎么了

TA贡献1454条经验 获得超7个赞

将 jsonData 数组作为摘要函数的输入传递。


function groupByRegistrationDate(array) {

    const months = [

        "Jan",

        "Feb",

        "March",

        "April",

        "May",

        "June",

        "July",

        "August",

        "Sept",

        "Oct",

        "Nov",

        "Dec"

    ];


    return array.reduce((agg, current) => {

        const { registration_time, sex } = current;

        const month = months[new Date(registration_time).getMonth()];

        (agg[month] = agg[month] || []).push(sex.toLowerCase());

        return agg;

    }, {});

}


function preprocess(data) {

    return data.map((entry) => entry.data);

}


function summarize(jsonData) {

    const aggregatedData = groupByRegistrationDate(preprocess(jsonData));

    Object.entries(aggregatedData).forEach(([month, registrations]) => {

        const maleRegs = registrations.filter(x => x === 'male').length;

        const femaleRegs = registrations.length - maleRegs;


        console.log(`${month} - Male ${maleRegs} Female ${femaleRegs}`);

    });

}


summarize(jsonData);


查看完整回答
反对 回复 6天前
?
汪汪一只猫

TA贡献1550条经验 获得超8个赞

global.fetch = require("cross-fetch");


fetch("https://pastebin.com/raw/fvJkWEk5")

  .then(response => {

    return response.json();

  })

  .then(data => {


    var months = [

      "Jan",

      "Feb",

      "March",

      "April",

      "May",

      "June",

      "July",

      "August",

      "Sept",

      "Oct",

      "Nov",

      "Dec"

    ];


    var data = data.map(users => {


      var subset = {

        registration_time: users["data"]["registration_time"],

        sex: users["data"]["sex"]

      };

      var sex = subset["sex"];

      var date = new Date(subset["registration_time"]);

      var m = date.getMonth();


      return {month: m, sex: sex};

    });


    var uniqueMonths = [];

    var monthlyData = [];


    data.map(x => {

      if(typeof uniqueMonths.find(m => m.month === x.month) === 'undefined') {

        uniqueMonths.push({month: x.month, female: 0, male: 0});

      }


      monthlyData.push({

        month: x.month,

        female: x.sex === 'Female' ? 1: 0,

        male: x.sex === 'Male' ? 1: 0

      });

    });


    uniqueMonths = uniqueMonths.sort((a, b) => a.month-b.month);


    uniqueMonths.map(m => {

      monthlyData.filter(d => d.month === m.month).map(x => {

        m.female += x.female;

        m.male += x.male;

      });


      console.log(`${months[m.month]} - male ${m.male} female ${m.female}`);

    });


  })

  .catch(err => {

    console.error(err);

  });


查看完整回答
反对 回复 6天前
?
ITMISS

TA贡献1499条经验 获得超8个赞

要以上述给定格式打印数据,请使用此选项,其中json_data是您的 JSON 对象


var months = [

    "Jan",

    "Feb",

    "March",

    "April",

    "May",

    "June",

    "July",

    "August",

    "Sept",

    "Oct",

    "Nov",

    "Dec"

  ];

  var months_data = [

      {

          "female" : 0,

          "male" : 0,

      },

      {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

    {

        "female" : 0,

        "male" : 0,

    },

  ]

  json_data.map((u,i) => {

      months_data[parseInt(u.data.registration_time.split("-")[1]) - 1][u.data.sex.toLowerCase()]++;

  })

  months_data.map((u,i) => {

    console.log(months[i] + "  male " +  u.male + " female " + u.female);

  })


查看完整回答
反对 回复 6天前
  • 3 回答
  • 0 关注
  • 10 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信