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

baltimoretim

I have an array of hashes that contain an array of items as the hash value. Here's the structure:

arr = [
  {:title => "String1", :link => ["URL1", "URL2"]},
  {:title => "String2", :link => ["URL3", "URL4", "URL5"]}
]

I'd like to add a key-value pair that counts the items in each :link like this:

arr = [
  {:title => "String1", :link => ["URL1", "URL2"], :link_count => 2},
  {:title => "String2", :link => ["URL3", "URL4", "URL5"]}, :link_count => 3 
]

I can get to the counts of each :link using this:

arr.map{|x| x[:link].count}

but I can't persist the count as a new key. Any ideas?

ray

You can simply do it by Array#each as below,

> arr.each { |h| h[:link_count] = h[:link].count }
# => [{:title=>"String1", :link=>["URL1", "URL2"], :link_count=>2}, {:title=>"String2", :link=>["URL3", "URL4", "URL5"], :link_count=>3}]  

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

How to convert array of hashes into a single hash and count repeated items?

分類Dev

Given a hash of arrays, how to create an array of hashes with each possible combo

分類Dev

How to count occurrences of strings in array of arrays?

分類Dev

Return specific values from array of hashes - JSON

分類Dev

How to count all values in a multidimensional array?

分類Dev

How to filter arrays depending on the values of arrays inside the array?

分類Dev

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

分類Dev

Unique count of array values

分類Dev

Get count from Array of arrays

分類Dev

How to remove array items between specific values that contains specific phrase

分類Dev

How to sum values column wise for Array of Arrays in Scala?

分類Dev

PHP - How to remove duplicate values from array (compare 2 arrays)

分類Dev

How to iterate through values in an array containing arrays in PHP

分類Dev

How to get the count of repeated key values of array object of consecutive elements?

分類Dev

Dealing with hashes inside arrays

分類Dev

Adding to and deleting hashes in arrays

分類Dev

Hashes in hashes that produce default array

分類Dev

Inserting values randomly into an array of arrays

分類Dev

How to group array of hashes and create nested array after keyword?

分類Dev

Count number of items in an array with a specific property value

分類Dev

Using Array Key => Value to track count of items

分類Dev

Count duplicate values in hash of an array

分類Dev

PHP - Count the values of an array not the keys?

分類Dev

How to view items in array

分類Dev

How to "number" array items?

分類Dev

MySQL to PostgreSQL - Find in array, and count array values

分類Dev

How to compare an array to an array of arrays?

分類Dev

How to insert an array into an array of arrays?

分類Dev

Take an array of pairs of strings and return an array of arrays of all connected items

Related 関連記事

  1. 1

    How to convert array of hashes into a single hash and count repeated items?

  2. 2

    Given a hash of arrays, how to create an array of hashes with each possible combo

  3. 3

    How to count occurrences of strings in array of arrays?

  4. 4

    Return specific values from array of hashes - JSON

  5. 5

    How to count all values in a multidimensional array?

  6. 6

    How to filter arrays depending on the values of arrays inside the array?

  7. 7

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

  8. 8

    Unique count of array values

  9. 9

    Get count from Array of arrays

  10. 10

    How to remove array items between specific values that contains specific phrase

  11. 11

    How to sum values column wise for Array of Arrays in Scala?

  12. 12

    PHP - How to remove duplicate values from array (compare 2 arrays)

  13. 13

    How to iterate through values in an array containing arrays in PHP

  14. 14

    How to get the count of repeated key values of array object of consecutive elements?

  15. 15

    Dealing with hashes inside arrays

  16. 16

    Adding to and deleting hashes in arrays

  17. 17

    Hashes in hashes that produce default array

  18. 18

    Inserting values randomly into an array of arrays

  19. 19

    How to group array of hashes and create nested array after keyword?

  20. 20

    Count number of items in an array with a specific property value

  21. 21

    Using Array Key => Value to track count of items

  22. 22

    Count duplicate values in hash of an array

  23. 23

    PHP - Count the values of an array not the keys?

  24. 24

    How to view items in array

  25. 25

    How to "number" array items?

  26. 26

    MySQL to PostgreSQL - Find in array, and count array values

  27. 27

    How to compare an array to an array of arrays?

  28. 28

    How to insert an array into an array of arrays?

  29. 29

    Take an array of pairs of strings and return an array of arrays of all connected items

ホットタグ

アーカイブ