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

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

基于嵌套鍵值計(jì)算對(duì)象

基于嵌套鍵值計(jì)算對(duì)象

慕慕森 2021-08-20 18:34:59
我有這個(gè)嵌套的json,我想搜索一個(gè)值,然后返回找到該值的整個(gè)對(duì)象并計(jì)算這些對(duì)象并使用映射和過(guò)濾器重新創(chuàng)建對(duì)象。我不知道該怎么做,因?yàn)槲沂蔷幊绦率?。在示?json 中,我想搜索水果的值,然后計(jì)算所有帶有日期的水果值的對(duì)象總數(shù)。示例 Json 對(duì)象:var sampleData = [ {   fruitData: {     fruit: 'apple',     price: 23   },   dateCreated: '2019-07-23T00:20:36.535Z' }, {   fruitData: {     fruit: 'apple',     price: 36    },   dateCreated: '2019-07-23T00:30:32.135Z' }, {   fruitData: {     fruit: 'apple',     price: 36    },   dateCreated: '2019-07-24T00:10:36.115Z' }, {   fruitData: {     fruit: 'mango',     price: 40    },   dateCreated: '2019-07-24T01:25:32.835Z' },]我希望輸出是這樣的,并在搜索水果的價(jià)值后進(jìn)行計(jì)數(shù)var filteredData= [ {   '07-23-2019': {       'apple': 2   },   '07-24-2019': {       'apple': 1,       'mango': 1   } }]感謝那些愿意提供幫助的人
查看完整描述

2 回答

?
慕哥6287543

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

假設(shè)JSON是結(jié)構(gòu)化的,我們可以得到fruits的值:


function fruitCount(sampleData) {

    var results = {};


    // sampleData is a list, so for each item

    sampleData.forEach((container) => {


        // Get the relevant data from the subfields.

        let date = container.dateCreated;

        let fruit = container.fruitData.fruit;


        // If the date has been found before, we'll reuse it.

        // If the fruit has been found before, we'll increment it.

        // If the fruit has NOT been found before, we'll insert it with a

        //     value of 1.

        if (date in results) {

            results[date][fruit] = results[date][fruit] + 1 || 1;


        // The date has not been found before, so create it.

        } else {

            results[date] = {fruit: 1};

        }

    });


    return results;

}

if (date in results) {

    results[date][fruit] = results[date][fruit] + 1 || 1;

相當(dāng)于


if (date in results) {

    if (fruit in results[date]) {

        results[date][fruit]++;

    } else {

        results[date][fruit] = 1;

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-08-20
?
慕森卡

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

const sampleData = [

    {

        fruitData: {

            fruit: 'apple',

            price: 23

        },

        dateCreated: '07-23-2019'

    },

    {

        fruitData: {

            fruit: 'apple',

            price: 36

        },

        dateCreated: '07-23-2019'

    },

    {

        fruitData: {

            fruit: 'mango',

            price: 40

        },

        dateCreated: '07-23-2019'

    },

];



function groupData(data, key) {

    const objs = {};

    for (const obj of data) {

        objs[obj[key].fruit] = (objs[obj[key].fruit] || 0) + 1;

    }

    return objs;

}


const data = groupData(sampleData, 'fruitData');

如果您希望數(shù)據(jù)為數(shù)組,則將最后一行更改為此


const data = [groupData(sampleData, 'fruitData')];


查看完整回答
反對(duì) 回復(fù) 2021-08-20
  • 2 回答
  • 0 關(guān)注
  • 159 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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