Retrieving an array from a multidimensional array - ruby-on-rails

My response from an API is coming in like:
[0] = "[Identity[id=95571, type=start, userId=d12345, processId=95567]]"
[1] = "[Identity[id=95572, type=start, userId=d67890, processId=95568]]"
etc
Lets call the above arr.
I want to retrieve all the userIds
I have tried:
all_users = arr.collect {|ind| ind[2]}
But this is obviously incorrect. What am I missing?
Thanks

Your array elements are strings, so can use string methods to extract parts from them, e.g.
arr.map { |e| e.match(/\[id=(/d+),/)[1] }

Related

How to filter array of array based on another subarray

I am trying to filter an array of array, based on another subarray. I am giving example below to make the requirement more clear. Please note that order of filterArray is important. I can iterate myArrayofArray using for loop, and then compare the element of each iterated element with filterArray. But I think Filter can be used in this case to get the resultArray. But a bit confused, how I'll implement it.
myArrayofArray = [[1,0,2], [1,2,0], [1,3,4], [1,2,1]]
filterArray = [1,0]
resultArray = [1,0,2]
The easiest way would be
let result = myArrayofArray.filter { $0.starts(with: filterArray) }
This will return a [[Int]] with zero or more matching arrays.

Swift 3: how to sort Dictionary's key and value of struct

This is my Struct,by Swift 3. I know the Dictionary is not stored sequence like an Array and that is my problem. I want to get my Dictionary sequence as I set in ViewArray. I can get the ctC Dictionary, but how can i sort the keys or values as i set in ViewArrayplease and appreciate the help.
struct CTArray {
var ctname: String
var ctkey: String
var ctC: [String:String]
}
var ViewArray:[CTArray] = []
ViewArray += [CTArray(ctname: "kerish", ctkey: "KH", ctC: ["mon":"Apple", "kis":"aone", "Bat":"Best", "orlno":"bOne"])]
ViewArray += [CTArray(ctname: "tainers", ctkey: "TNN", ctC: ["letGor":"one", "washi":"testing", "monk":"lasth"])]
ViewArray += [CTArray(ctname: "techiu", ctkey: "TCU", ctC: ["22":"tt", "wke":"303", "lenth":"highest"])]
i want to show them in my TableView Cell sorted like these:
the ViewArray[0].ctC.key sorted like [mon, kis, Bat, orlno]
the ViewArray[1].ctC.key sorted like [letGor, washi, monk]
the ViewArray[2].ctC.value sorted like [tt, 303, highest]
It is not clear to me what you are asking, but I'll offer this in case it helps.
I know the Dictionary is not stored sequence like an Array and that is my problem.
If you want to access a dictionary by a certain order of its keys then you can create an array of just the keys in the order you required and use that to access the dictionary. An example is probably easier to follow:
Starting with one of your dictionaries:
let dict = ["mon":"Apple", "kis":"aone", "Bat":"Best", "orlno":"bOne"]
print(dict)
this output:
["Bat": "Best", "kis": "aone", "orlno": "bOne", "mon": "Apple"]
which is not what you want. Now introduce a key array and use that to access the dictionary:
let keyOrder = ["mon", "kis", "Bat", "orlno"]
for key in keyOrder
{
print("\(key): \(dict[key]!)")
}
this outputs:
mon: Apple
kis: aone
Bat: Best
orlno: bOne
which is the order you wish.
The same idea can be used anywhere you want to use/show/etc. the keys in a particular order, by using the keyOrder array as part of dictionary access you are making it appear as though the dictionary entries are "stored in sequence" as you put it.
HTH
var object1 = viewArray[0].ctC.flatMap({$0.key})
var object2 = viewArray[1].ctC.flatMap({$0.key})
var object3 = viewArray[2].ctC.flatMap({$0.value})
print(object1)
print(object2)
print(object3)
Outputs:
["Bat", "kis", "orlno", "mon"]
["letGor", "monk", "washi"]
["highest", "tt", "303"]

select in a parsed json, comparing id with an array of ids

i need to filter array result (get by parsed json).
if i know the exact id i can select in the json using
#my_art = ele_art.select { |articolo| articolo['id'] == 456 }
Now I have an array of ids called #myarray and i need to select in ele_art only the items with id in the array
Reading the array i have:
[279, 276]
i tried with
#my_art = ele_art.select { |articolo| articolo['id'] == #myarray }
or
#my_art = ele_art.select { |articolo| articolo['id'] in #myarray }
with no luck!
how can i solve?
#my_array is and array of ids, so, in that case you need to check if articolo['id'] is included in #myarray. For those cases, the Array class in Ruby has the include? method, which receives an object and returns true/false if the object is included or not in the array.
So, in your case try something like:
#my_art = ele_art.select { |articolo| #myarray.include?(articolo['id']) }

Converting array/hash to string and converting that string back to array/hash in ruby

I had this array lets say,
array = [{'key' => 0},1]
so now array[0]['key'] has value 0.
When I convert it to string like this:
array.to_s
Now the array is not an array its a string which is like this:
"[{'key' => 0},1]"
If I do array[0] now, it will output [
I want to convert this back to array so that I can use array[0]['key'] again.
Full Code:
array = [{'key' => 0},1]
array = array.to_s
puts array[0]['key'] #Should output 0 but it does not output anything
The thing is that I have already saved stuff like that in the database, so now I need to use that stuff back, so the only way is to parse the saved string (which was actually an array).
string = "[{'key' => 0},1]"
array = eval string
array[0]['key'] # => 0

How can I convert a single column of an array of Active Record objects into an array of strings?

I have an array of rails ActiveRecord objects, and I need to extract a single string column into an array. Is there an easy way to get ActiveRecord to return my simple array without writing a loop?
Currently I have:
myObjects = MyObject.all
myArray = []
myObjects.each do |obj|
myArray << obj.field_name
end
I'd like to have something like:
myArray = MyObject.all.give_me_the_array_of(:field_name)
You can use pluck
MyObject.pluck(:field_name)
Following one liner should work:
myArray = MyObject.all.map{|a| a.field_name}

Resources