'Searching through an array

Trying to learn how to think in jq script.

Given this data:

{
   "characters": [
       { "First": "Fred", "Last": "Weasley" },
       { "First": "George", "Last": "Weasley" },
       { "First": "Hermione", "Last": "Granger" },
       { "First": "Ron", "Last": "Weasley" },
       { "First": "Hagrid" },
       { "First": "Draco", "Last": "Malfoy" },
       { "First": "Molly", "Last": "Weasley" },
       { "First": "Voldemort" },
       { "First": "Lucius", "Last": "Malfoy" }
    ]
}

Find all characters with the same last name as "Ron". And no, you don't already know his last name.



Solution 1:[1]

You have an array of objects called "characters" each containing a "first" and "last" variable. Now to get the first one. Go characters[0].first which would return. "Fred" Or characters.[3].last would return "Weasley" Note the first entry in array is fetched by index 0. Make sense?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Turtle