Get count from Array of arrays

colin_dev256

I have an array of arrays below. With ES6, how can I get a count of each value Good, Excellent & Wow into a new array e.g [{name: Good, count: 4} {name: Excellent, count: 5}, {name:Wow, count:2}] in dynamic style. I am attempting to use Object.assign but I am failing to "unique" out the count of the key plus instead, I need to use an array as I am trying to render this out on the front end. Do I need to use reduce? how?

let k = 0
const stats = {}
const remarks = [
  [{name: "Good"}],
  [{name: "Good"}, {name: "Excellent"}],
  [{name: "Good"}, {name: "Excellent"}, {name: "Wow"}],
  [{name: "Good"}, {name: "Excellent"}, {name: "Wow"}],
  [{name: "Excellent"}],
  [{name: "Excellent"}]
]

remarks.forEach((arr) => {
  arr.map((e) => {
    Object.assign(stats, { [e.name]: k = k + 1 })
  })
})

console.log(stats);

Output:

stats: {Good: 8, Excellent: 11, Wow: 9}

Which is Incorrect plus I need to use an array.

Expected output:

[{name: Good, count: 4} {name: Excellent, count: 5}, {name:Wow, count:2}]

Taki

Flatten the array of arrays and reduce it starting with an object like : { Good: 0, Excellent: 0, Wow: 0}

then .map the Object.entries of the result to transform it to an array :

const remarks = [
  [{ name: "Good" }],
  [{ name: "Good" }, { name: "Excellent" }],
  [{ name: "Good" }, { name: "Excellent" }, { name: "Wow" }],
  [{ name: "Good" }, { name: "Excellent" }, { name: "Wow" }],
  [{ name: "Excellent" }],
  [{ name: "Excellent" }]
];

const result = Object.entries(
  remarks.flat().reduce(
    (all, { name }) => {
      all[name] += 1;
      return all;
    },
    { Good: 0, Excellent: 0, Wow: 0 }
  )
).map(([name, count]) => ({ name, count }));

console.log(result);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

mongodb - Get one array from two arrays in collection

分類Dev

group array and get count

分類Dev

How to count items in arrays of values in an array of hashes

分類Dev

How to count occurrences of strings in array of arrays?

分類Dev

get an array of arrays with unique elements

分類Dev

How to get array of array value count?

分類Dev

Count elements from an array in php

分類Dev

How to get common values from 4 multidimensional arrays using array_intersect

分類Dev

Get all the nested arrays from main array and merge each of them according to keys Knockout.Js

分類Dev

Remove array from array of arrays using Ramda?

分類Dev

Iterate an array of arrays for a match from another array

分類Dev

NODEJS: Return array of arrays from dialog openDirectory

分類Dev

How to get arrays on output from MongoDB aggregate

分類Dev

Lodash get leafs from the mixed arrays and objects

分類Dev

Get invoice total from a collection with arrays

分類Dev

Get computed value from two observable arrays

分類Dev

How to initiate a dict from a array with count?

分類Dev

Can't get message from the database table. ErrorException say:count(): Parameter must be an array or an object that implements Countable

分類Dev

Get like count from a facebook page

分類Dev

Check an array for duplicate values, get value and get count

分類Dev

Efficiently create arrays from a next n elements from a array

分類Dev

Efficiently create arrays from a next n elements from an array

分類Dev

Retrieving data from multidimensional array from arrays.xml

分類Dev

get a set of values from an array

分類Dev

Get data from array in Controller

分類Dev

Getting empty array from get

分類Dev

Get Array From XML Nodes

分類Dev

MySQL get rows from array

Related 関連記事

  1. 1

    How to get the combination of array values from nested arrays in an array of objects

  2. 2

    mongodb - Get one array from two arrays in collection

  3. 3

    group array and get count

  4. 4

    How to count items in arrays of values in an array of hashes

  5. 5

    How to count occurrences of strings in array of arrays?

  6. 6

    get an array of arrays with unique elements

  7. 7

    How to get array of array value count?

  8. 8

    Count elements from an array in php

  9. 9

    How to get common values from 4 multidimensional arrays using array_intersect

  10. 10

    Get all the nested arrays from main array and merge each of them according to keys Knockout.Js

  11. 11

    Remove array from array of arrays using Ramda?

  12. 12

    Iterate an array of arrays for a match from another array

  13. 13

    NODEJS: Return array of arrays from dialog openDirectory

  14. 14

    How to get arrays on output from MongoDB aggregate

  15. 15

    Lodash get leafs from the mixed arrays and objects

  16. 16

    Get invoice total from a collection with arrays

  17. 17

    Get computed value from two observable arrays

  18. 18

    How to initiate a dict from a array with count?

  19. 19

    Can't get message from the database table. ErrorException say:count(): Parameter must be an array or an object that implements Countable

  20. 20

    Get like count from a facebook page

  21. 21

    Check an array for duplicate values, get value and get count

  22. 22

    Efficiently create arrays from a next n elements from a array

  23. 23

    Efficiently create arrays from a next n elements from an array

  24. 24

    Retrieving data from multidimensional array from arrays.xml

  25. 25

    get a set of values from an array

  26. 26

    Get data from array in Controller

  27. 27

    Getting empty array from get

  28. 28

    Get Array From XML Nodes

  29. 29

    MySQL get rows from array

ホットタグ

アーカイブ