JavaScript consolidating values in a list of objects based on an attribute which is the same

Bit of background, this comes from a submitted form that I used serializeArray() on I have a list of objects like so.

[
  {name: 0, value: 'waffles'},
  {name: 0, value: 'pancakes'},
  {name: 0, value: 'french toast'},
  {name: 1, value: 'pancakes'}
]

I want to take all things that have the same name attribute and put them together. EG,

[
  {name: 0, value: ['waffles', 'pancakes', 'french toast']},
  {name: 1, value: ['pancakes']}
]

how would one go about this? All the things I've tried only result in one answer being shown for each name key.

Answer:1

This should do it:

var newlist = _.map(_.groupBy(oldlist, "name"), function(v, n) {
    return {name: n, values: _.pluck(v, "value")};
});
Answer:2

Here's the best I could come up with:

http://jsfiddle.net/Z6bdB/

var arr = [
  {name: 0, value: 'waffles'},
  {name: 0, value: 'pancakes'},
  {name: 0, value: 'french toast'},
  {name: 1, value: 'pancakes'}
]

var obj1 = {};

$.each(arr, function(idx, item) { 
    if (obj1[item.name]) {
        obj1[item.name].push(item.value);  
    } else {
        obj1[item.name] = [item.value];
    }
});

var result = [];

for(var prop in obj1) {
    result.push({
        name: prop,
        value: obj1[prop]
    });
}

console.log(result);
Answer:3

I am one of those guys that uses native functions:

var food =  [
     {name: 0, value: 'waffles'},
     {name: 0, value: 'pancakes'},
     {name: 0, value: 'french toast'},
     {name: 1, value: 'pancakes'}
 ];

 var result = food.reduce(function(res,dish){
     if (!res.some(function(d){return d.name === dish.name })){
     var values = food.filter(function(d){ return d.name === dish.name }).map(function(d){ return d.value; });
     res.push({name: dish.name, value : values});
     }
     return res;
 }, []);
 console.log(result);
Answer:4

I have a checkbox section that users can select to add features. I need each input's value to add to a sum to be presented in the #payment-total and #payment-rebill section. Essentially, if they ...

I have a checkbox section that users can select to add features. I need each input's value to add to a sum to be presented in the #payment-total and #payment-rebill section. Essentially, if they ...

  1. javascript checkbox checked event
  2. javascript checkbox checked value
  3. javascript checkbox checked or not
  4. javascript checkbox checked onclick
  5. javascript checkbox checked not working
  6. javascript checkbox checked and unchecked event
  7. javascript checkbox checked event listener
  8. javascript checkbox checked unchecked
  9. javascript checkbox checked jquery
  10. javascript checkbox checked value push array
  11. javascript checkbox checked event example
  12. javascript checkbox checked undefined
  13. javascript checkbox checked check
  14. javascript checkbox checked default
  15. javascript checkbox checked returns undefined
  16. javascript checkbox checked function
  17. javascript checkbox checked condition
  18. javascript checkbox checked count
  19. javascript checkbox checked true false
  20. javascript checkbox checked state

I'm learning Ajax and I'm confused about something. In a tutorial, these two lines are included document.myForm.time.value = ajaxRequest.responseText; //code <form name='myForm'> Name: <...

I'm learning Ajax and I'm confused about something. In a tutorial, these two lines are included document.myForm.time.value = ajaxRequest.responseText; //code <form name='myForm'> Name: <...

In Sharepoint, when we load the sp.js file after that when we use given code then it gives the current site context. But how can i get the site context using url. var context = new SP.ClientContext....

In Sharepoint, when we load the sp.js file after that when we use given code then it gives the current site context. But how can i get the site context using url. var context = new SP.ClientContext....

  1. clientcontext using c#
  2. using clientcontext sharepoint c#
  3. using clientcontext sharepoint
  4. using client context

I am working with PHP to display my log in form. Right now, the form shows "Username" but the text doesn't go away when the user starts to type. How can I have the text go away when the user starts to ...

I am working with PHP to display my log in form. Right now, the form shows "Username" but the text doesn't go away when the user starts to type. How can I have the text go away when the user starts to ...

  1. show text inside input field
  2. show text inside svg
  3. show text inside div
  4. show text inside password input
  5. show text inside css
  6. show text inside html
  7. tableau show text inside bar
  8. show image inside text box