第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

使用腳本進(jìn)行 JSON 操作

使用腳本進(jìn)行 JSON 操作

慕桂英3389331 2022-09-23 16:16:47
我有包含數(shù)據(jù)的 json 文件。我想像這樣打印輸出:jan - male 1 female 2feb - male 1 female 1march - 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);  });這似乎是一項(xiàng)簡單的任務(wù),但我發(fā)現(xiàn)很難實(shí)現(xiàn)。
查看完整描述

3 回答

?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

將 jsonData 數(shù)組作為摘要函數(shù)的輸入傳遞。


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);


查看完整回答
反對 回復(fù) 2022-09-23
?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

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);

  });


查看完整回答
反對 回復(fù) 2022-09-23
?
ITMISS

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊

要以上述給定格式打印數(shù)據(jù),請使用此選項(xiàng),其中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);

  })


查看完整回答
反對 回復(fù) 2022-09-23
  • 3 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號