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

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

基于嵌套鍵值計算對象

基于嵌套鍵值計算對象

慕慕森 2021-08-20 18:34:59
我有這個嵌套的json,我想搜索一個值,然后返回找到該值的整個對象并計算這些對象并使用映射和過濾器重新創(chuàng)建對象。我不知道該怎么做,因為我是編程新手。在示例 json 中,我想搜索水果的值,然后計算所有帶有日期的水果值的對象總數(shù)。示例 Json 對象: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' },]我希望輸出是這樣的,并在搜索水果的價值后進行計數(shù)var filteredData= [ {   '07-23-2019': {       'apple': 2   },   '07-24-2019': {       'apple': 1,       'mango': 1   } }]感謝那些愿意提供幫助的人
查看完整描述

2 回答

?
慕哥6287543

TA貢獻1831條經(jīng)驗 獲得超10個贊

假設(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;

    }

}


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

TA貢獻1806條經(jīng)驗 獲得超8個贊

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


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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