3

EDITED I have this nested json and I want to search for a value then return the whole object where this value was found and count those object and recreate the object using map and filter. I don't know what to do it since I'm kinda newbie in programming.

In the example json I want to search for values of fruits and then count all the total objects with value of those fruits with dates.

Sample Json object:

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'
 },
]

I expect the output to be like this and count after searching for value of fruits

var filteredData= [
 {
   '07-23-2019': {
       'apple': 2
   },
   '07-24-2019': {
       'apple': 1,
       'mango': 1
   }
 }
]

Thank you for those who will help

2
  • Is the data always in that format? Commented Jul 24, 2019 at 21:33
  • yes, it will be always in this format Commented Jul 25, 2019 at 1:06

2 Answers 2

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

if you want data to be an array then change the last line to this

const data = [groupData(sampleData, 'fruitData')];
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Jamal, this works too.. thanks for another solution. but can this be achieve using array.filter() and array.map()? I want to up vote but I cant because I am below 15 reputation.
sorry i change the output see the sample on my edited topic, I forgot that the date should also be included..
1

Assuming that the JSON is structured, we can get the value of 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;

Is equivalent to

if (date in results) {
    if (fruit in results[date]) {
        results[date][fruit]++;
    } else {
        results[date][fruit] = 1;
    }
}

3 Comments

Thanks Michael, this works too.. thanks for another solution. but can this be achieve using array.filter() and array.map()? I want to up vote but I cant because I am below 15 reputation.
sorry i change the output see the sample on my edited topic, I forgot that the date should also be included..
thank you, one last thing, can you explain to me each line of the code? Using comments in the code It will be a big help to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.