JavaScript Split object key/value pairs into separate objects split object key value,split object in keyshot,javascript split

Sounds like you want Array.prototype.reduce().

let grades = [5,5,6,7,6,7,9,10,8,6];

let finalSpread = grades.reduce((spread, grade) => {
  let count = spread[grade] || 0;
  spread[grade] = ++count;
  return spread;
}, {});

Seeing as you want a final value that's essentially a map of key-values, I'd recommend finalSpread being an object like above.



Update:

If it's imperative that it be an array for whatever reason you could:

a. Add finalSpread = Object.entries(finalSpread) at the end (note: each grade will actually be a string)

b. Just refactor the function in reduce to check if they array of [grade, count] already exists and either add it or increment it.

c. use a Map instead of an array as the reduce accumulator (if you need the grades to also be numbers - Map allows non-string keys) - then cast it to an array, i.e:

let finalSpread = grades.reduce((spread, grade) => {
  let count = spread.get(grade) || 0;
  spread.set(grade, ++count);
  return spread;
}, new Map());
finalSpread = Array.from(finalSpread);

(note: map and Array.from() have limited browser support.)

Answer:1
var arr = [];
Object.keys(grades).forEach((key) => {
    var newObj = {};
    newObj[key] = grades[key];
    arr.push(newObj);
});

Using ES6 it could be shorter:

var arr = [];
Object.keys(grades).forEach((key) => {
    arr.push({[key]: grades[key});
});
Answer:2

You could map the keys with the values. The order does not reflect the order of insertation of the object.

var object = { 5: 1, 6: 3, 2: 4 },
    result = Object
        .keys(object)
        .map(k => ({ [k]: object[k] }));
    
console.log(result);
Answer:3

I am using a layout list with Masonry (and animOnScroll on top of it). I want to add bootstrap dropdowns to my list items, but Masonry inserts directly into every dropdown <li> DOM node a style="...

I am using a layout list with Masonry (and animOnScroll on top of it). I want to add bootstrap dropdowns to my list items, but Masonry inserts directly into every dropdown <li> DOM node a style="...

I have a javascript array which get created dynamically in this format. [{prdName: "Testing2"}, {prdName: "Testing2,Testing3"}, {markets: "Testing5"}, {markets: "Testing5,Testing6"}] I want to ...

I have a javascript array which get created dynamically in this format. [{prdName: "Testing2"}, {prdName: "Testing2,Testing3"}, {markets: "Testing5"}, {markets: "Testing5,Testing6"}] I want to ...

  1. removing duplicate value in excel
  2. remove duplicate value from array in php
  3. remove duplicate value from array
  4. remove duplicate value from array javascript
  5. remove duplicate value from array in java
  6. remove duplicate value from list in python
  7. remove duplicate value from array in swift 4
  8. remove duplicate value from array in jquery
  9. how to remove duplicate values in sql
  10. remove duplicate value from list in c#
  11. remove duplicate value from list in java
  12. remove duplicate value from array swift
  13. remove duplicate value in array js
  14. remove duplicate value from multidimensional array in php
  15. remove duplicate value from arraylist in java
  16. remove duplicate value from associative array in php
  17. remove duplicate value from arraylist in android
  18. remove duplicate value online
  19. remove duplicate value in sql query
  20. remove duplicate value from array in c

Hi I want the form I'm making to submit but not before the users press ENTER key without accepting terms and conditions. I already made that but in Safari when the user press ENTER, the alert ...

Hi I want the form I'm making to submit but not before the users press ENTER key without accepting terms and conditions. I already made that but in Safari when the user press ENTER, the alert ...

  1. form submit alert javascript
  2. form submit alert in jquery
  3. form submit alert message
  4. submit form alert box
  5. html form submit alert
  6. php form submit alert
  7. form submit success alert
  8. form submit confirm alert
  9. javascript form submit alert box
  10. form submit after alert
  11. form submit sweet alert
  12. bootstrap form submit alert
  13. mvc form submit alert
  14. form submit onclick alert
  15. on submit alert form input
  16. submit form using sweet alert
  17. contact form 7 submit alert
  18. submit form alertify
  19. form submit 후 alert

I use jquery.flexslider plugin, which works fine. But for screens < 600px I do not want to load the plugin. I tried several scripts and ended with the following, which I thought works best. I ...

I use jquery.flexslider plugin, which works fine. But for screens < 600px I do not want to load the plugin. I tried several scripts and ended with the following, which I thought works best. I ...