JavaScript Group nested array in Javascript group nested array mongodb,group nested array,mongodb group nested

I have an array of data that is being used for some visualisation and is in the below format

var Dataset1 =  [
      {
        "commentBy" : "saurabh",
        "comment" : "Testing",
        "datestamp" : "07/07/2017",
        "weekcount" : 1
      },
      
       {
        "commentBy" : "raman",
        "comment" : "Planning",
        "datestamp" : "07/07/2017",
        "weekcount" : 1
      },
       {
        "commentBy" : "Execution",
        "comment" : "Alfa Beta",
        "datestamp" : "07/07/2017",
        "weekcount" : 2
      },
        {
        "commentBy" : "Execution",
        "comment" : "Zseta Gama",
        "datestamp" : "07/07/2017",
        "weekcount" : 2
      } 
        ]

//although i have tried writing this function but this is not giving me the desired result.


var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
 
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};
var groubedByTeam=groupBy(Dataset1, 'weekcount')
console.log(groubedByTeam);
Answer:1
const formatted = [];

Dataset1.forEach((data) => {
  const { weekcount, comment, commentBy, datestamp } = data;
  let obj = formatted.find((item) => item.weekcount === weekcount);

  if (!obj) {
    formatted.push({
      weekcount,
      grouped: [{
        comment,
        commentBy,
        datestamp
      }]
    })
  } else {
    obj.grouped.push({
      comment,
      commentBy,
      datestamp
    });
  }
});

const Dataset1 = [{
  "commentBy": "saurabh",
  "comment": "Testing",
  "datestamp": "07/07/2017",
  "weekcount": 1
}, {
  "commentBy": "raman",
  "comment": "Planning",
  "datestamp": "07/07/2017",
  "weekcount": 1
}, {
  "commentBy": "Execution",
  "comment": "Alfa Beta",
  "datestamp": "07/07/2017",
  "weekcount": 2
}, {
  "commentBy": "Execution",
  "comment": "Zseta Gama",
  "datestamp": "07/07/2017",
  "weekcount": 2
}];

const formatted = [];

Dataset1.forEach((data) => {
  const { weekcount, comment, commentBy, datestamp } = data;
  let obj = formatted.find((item) => item.weekcount === weekcount);

  if (!obj) {
    formatted.push({
      weekcount,
      grouped: [{
        comment,
        commentBy,
        datestamp
      }]
    })
  } else {
    obj.grouped.push({
      comment,
      commentBy,
      datestamp
    });
  }
});

console.log(formatted);
Answer:2

Here is a clean way to group the data, you should be able to figure out how to format it the way you want with this as a starting point.

grouped = {}

Dataset1.forEach(function(item, index){

    if (!grouped[item.weekcount]) grouped[item.weekcount] = [];
    grouped[item.weekcount].push(item);

});

grouped is an object keyed with the weekcount. If a certain weekcount doesn't exist as a key in the object, an empty array is created and then the data is pushed to it. On later iterations data with the same weekcount is added to the existing array.

Answer:3

You could check each weekcount from 0 to max, and filter your array. It could be something like this:

var Dataset1 =  [
      {
        "commentBy" : "saurabh",
        "comment" : "Testing",
        "datestamp" : "07/07/2017",
        "weekcount" : 1
      },

       {
        "commentBy" : "raman",
        "comment" : "Planning",
        "datestamp" : "07/07/2017",
        "weekcount" : 1
      },
       {
        "commentBy" : "Execution",
        "comment" : "Alfa Beta",
        "datestamp" : "07/07/2017",
        "weekcount" : 2
      },
        {
        "commentBy" : "Execution",
        "comment" : "Zseta Gama",
        "datestamp" : "07/07/2017",
        "weekcount" : 2
      } 
        ]

var maxWeekCount = 3;
var result = []
for(var i=0; i<maxWeekCount; i++){
  var group = Dataset1.filter(obj => obj.weekcount === i)
  if(group.length) {
    result.push({
      weekCount: i,
      grouped: group
    })
  }
}

console.log(result)
Answer:4

Use a helper object, that maintains a reference to the weekcount objects, to reduce the array to the grouped structure.

var Dataset1 = [{"commentBy":"saurabh","comment":"Testing","datestamp":"07/07/2017","weekcount":1},{"commentBy":"raman","comment":"Planning","datestamp":"07/07/2017","weekcount":1},{"commentBy":"Execution","comment":"Alfa Beta","datestamp":"07/07/2017","weekcount":2},{"commentBy":"Execution","comment":"Zseta Gama","datestamp":"07/07/2017","weekcount":2}];

var helperMap = {};

var result = Dataset1.reduce(function(arr, obj) {
  var current = helperMap[obj.weekcount];
  
  if(!current) {
    current = { 
      weekcount: obj.weekcount,
      grouped: [] 
    };
    
   helperMap[obj.weekcount] = current;
    
    arr.push(current);
  }

  current.grouped.push({
    commentBy: obj.commentBy,
    comment: obj.comment,
    datestamp: obj.datestamp
  });
  
  return arr;
}, []);

console.log(result);
Answer:5

I have only been learning javascript for 2 weeks, so apologies if my question seems weird/doesn't make sense. I'm learning the basics of arrays, and to help me learn I like to practise and play around ...

I have only been learning javascript for 2 weeks, so apologies if my question seems weird/doesn't make sense. I'm learning the basics of arrays, and to help me learn I like to practise and play around ...

  1. javascript inserting data into html table
  2. javascript append data to array
  3. javascript append data
  4. javascript append data to object
  5. javascript append data to div
  6. javascript append data to json object
  7. javascript formdata append
  8. javascript append data to table
  9. javascript append data to csv file
  10. javascript append data attribute
  11. javascript append data to file
  12. javascript append data to text file
  13. javascript append data to json
  14. javascript adding data to object
  15. javascript append data to element
  16. javascript adding data attributes
  17. javascript append data to json array
  18. javascript append data to list
  19. javascript append data to variable
  20. javascript adding data to table

Good morning everyone, I have been working on this all night and I need some assistance. I am trying to use JavaScript to dynamically change a log in button's color when I log in to my site. It seems ...

Good morning everyone, I have been working on this all night and I need some assistance. I am trying to use JavaScript to dynamically change a log in button's color when I log in to my site. It seems ...

  1. html javascript dynamic table
  2. html javascript dynamic list
  3. html javascript dynamic form
  4. html javascript dynamic content
  5. html javascript dynamic drop down list
  6. html javascript dynamic select
  7. html javascript dynamic id
  8. html javascript dynamic text
  9. html javascript dynamic drop down
  10. html javascript dynamic variable
  11. html javascript dynamic website
  12. html javascript dynamic webpage
  13. html javascript dynamic button
  14. html javascript dynamic text field
  15. html dynamic javascript loading
  16. javascript html dynamic
  17. javascript dynamic html elements
  18. javascript dynamic html template
  19. dynamic html javascript tutorial
  20. javascript dynamic html event

I think I'm over-complicating things for a beginner but all I wanted to do was add addEventListener at once to four of my buttons. Then pass on that event, to a new function which checks which button ...

I think I'm over-complicating things for a beginner but all I wanted to do was add addEventListener at once to four of my buttons. Then pass on that event, to a new function which checks which button ...

  1. audio.pause not working
  2. audio_play_sound not working gamemaker
  3. javascript audio pause not working
  4. html5 audio pause not working

I would like to show value in select option which value was query from database. And this value should be use in another functions, not to show only. This is html script with JQM. <div data-role=...

I would like to show value in select option which value was query from database. And this value should be use in another functions, not to show only. This is html script with JQM. <div data-role=...

  1. show that value of g decreases with height