I have an array of objects that looks like this:
[
{
"field name" => "Account number",
"data type" => "number",
"mneumonic" => "ACTNUM",
"field number" => "027"
},
{
"field name" => "Warning",
"data type" => "code",
"mneumonic" => "WARN1",
"field number" => "034:01"
},
{
"field name" => "Warning",
"data type" => "code",
"mneumonic" => "WARN2",
"field number" => "034:02"
},
.....
]
I need to search through the array and mark duplicates based on the "field name" property. For this, I could use something like uniq { |i| i["field name"] }
However, for any duplicate items that are found, the item that ends up not being deleted needs to have a property added to it: multiple => true. I do not care which object ends up being the one that stays in the array, so long as it is marked with this property. So, running the function on the example above might produce:
[
{
"field name" => "Account number",
"data type" => "number",
"mneumonic" => "ACTNUM",
"field number" => "027",
},
{
"field name" => "Warning",
"data type" => "code",
"mneumonic" => "WARN1",
"field number" => "034:01",
"multiple" => true
},
.....
]
Besides the removal of duplicates, I also need to be sure that the array's order is not affected by the function.
What is the best way to go about this?
ArrayandHash, probably two of the most commonly used classes. You are not referring to some any particular methods, either. Any Rubyist knows where to find them, so I think it adds little information to your question.