I have this array right here and I need to get the "id" of each object
[{ id: 1, points: 60 }, { id: 2, points: 20 }, { id: 3, points: 95 }, { id: 4, points: 75 }]
customers = [{ id: 1, points: 90 }, { id: 2, points: 20 }, { id: 3, points: 70 }, { id: 4, points: 40 }, { id: 5, points: 60 }, { id: 6, points: 10}]
I know how to go through the whole array with
#scores.each_with_index{ |score, index| }
However, I haven't found a way to get the objects's points.
Perhaps you are looking for the following.
customers = [
{ id: 1, points: 90 }, { id: 2, points: 20 },
{ id: 3, points: 70 }, { id: 4, points: 40 },
{ id: 5, points: 60 }, { id: 6, points: 10}
]
h = customers.each_with_object({}) do |g,h|
id, points = g.values_at(:id, :points)
h[id] = points
end
#=> {1=>90, 2=>20, 3=>70, 4=>40, 5=>60, 6=>10}
This allows you to easily extract information of interest, such as the following.
h.keys
#=> [1, 2, 3, 4, 5, 6]
h.values
#=> [90, 20, 70, 40, 60, 10]
h[2]
#=> 20
h.key?(5)
#=> true
h.key?(7)
#=> false
h.value?(70)
#=> true
h.value?(30)
#=> false
What you called score is actually an hash like { id: 1, points: 60 } and I'm going to call it item
So, let's try
#scores.each_with_index do |item, index|
puts "#{index + 1}: id #{item[:id]}, points #{item[:points]}"
end
So, I have this array right here and I need to get the id of each object
In order to transform each element of a collection, you can use Enumerable#map (or in this case more precisely Array#map):
customers.map { _1[:id] }
#=> [1, 2, 3, 4, 5, 6]
This given construct is an array of objects so we need to individually iterate through each element and print out the value present in the objects. The following code shows how we can do it:
customers.each{|obj| p obj[:id].to_s+" "+ obj[:points].to_s }
Here we iterate through each element and print out individual entities of the hash using the obj[:id]/obj[:points] (obj being each individual object here.)
What about something like this?
customers.map(&:to_proc).map{ |p| [:id, :points].map(&p) }
=> [[1, 90], [2, 20], [3, 70], [4, 40], [5, 60], [6, 10]]
Related
results = [
{
:id=>2,
:start=> "3:30",
break: 30,
num_attendees: 14,
hello: {hi: 1}
},
{
id: 3,
start: "3: 40",
break: 40,
num_attendees: {hi: 2},
hello: 4
},
{
id: 4,
start: "4: 40",
break: 10,
num_attendees: 40
}
]
Is there a better way of doing this by avoiding multiple iteration?
results.each do |hash|
hash.each { |k, v|
hash[k] = "" if v.is_a? Hash
}
end
=> [{:id=>2, :start=>"3:30", :break=>30, :num_attendees=>14, :hello=>""}, {:id=>3, :start=>"3: 40", :break=>40, :num_attendees=>"", :hello=>4}, {:id=>4, :start=>"4: 40", :break=>10, :num_attendees=>40}]
Thanks
I have an array of custom objects, having ID values as properties. Now I have another array of selected objects (selected by IDs). How can I get the indices of the first array by filtering for the selected IDs (second array)?
I would like to avoid looping and think of a solution using compactMap or similar.
struct MyObject {
var ID: Int
var name: String
}
let myObjects = [
MyObject(ID: 3, name: "AAAA"),
MyObject(ID: 5, name: "BBBB"),
MyObject(ID: 12, name: "CCCC"),
MyObject(ID: 15, name: "DDDD"),
MyObject(ID: 20, name: "EEEE"),
MyObject(ID: 21, name: "GGGG"),
MyObject(ID: 22, name: "HHHH"),
MyObject(ID: 23, name: "IIII"),
]
let selectedIds = [5, 20, 23]
// How to get an array of indices for selectedIds
// Expected result [1, 4, 7]
myObjects.enumerated() // Pair-up elements and their offsets
.filter { selectedIds.contains($0.element.ID) } // Get the ones you want
.map { $0.offset } // Extract the offsets
You could filter the indices
let selectedIndices = myObjects.indices.filter{selectedIds.contains(myObjects[$0].ID)}
I am looking for way to sort elements in an array based on the classes of the elements in another array. I have:
order = [String, Integer, NilClass ,TrueClass]
arry = [1, 2, 3, 4, 5, true, false, nil, 34, nil, 'Hello', 'World']
In order, the order of the elements is dynamic. The result should be:
result_arry = ['Hello', 'World', 1, 2, 3, 4, 5, 34, nil, nil, true, false]
How can I sort arry based on the class of the elements in order?
I tried:
hash = arry.group_by {|n| n.class }
# => {Fixnum=>[1, 2, 3, 4, 5, 34], TrueClass=>[true], FalseClass=>[false], NilClass=>[nil, nil], String=>["Hello", "World"]}
Using group_by is a good approach, because it keeps the element's order:
hash = arry.group_by(&:class)
#=> {
# Integer => [1, 2, 3, 4, 5, 34],
# TrueClass => [true],
# FalseClass => [false],
# NilClass => [nil, nil],
# String => ["Hello", "World"]
# }
We can use sort_by to sort the hash by its keys based on each key's index in the order array. If a class is missing from orders, we use the array's size as a fallback to have it sorted last:
sorted = hash.sort_by { |k, _| order.index(k) || order.size }
#=> [
# [String, ["Hello", "World"]],
# [Integer, [1, 2, 3, 4, 5, 34]],
# [NilClass, [nil, nil]],
# [TrueClass, [true]],
# [FalseClass, [false]]
# ]
Finally, flat_map extracts each element's last part:
sorted.flat_map(&:last)
#=> ["Hello", "World", 1, 2, 3, 4, 5, 34, nil, nil, true, false]
You can also use sort_by without prior grouping, but it may shuffle elements with the same class:
arry.sort_by { |e| order.index(e.class) || order.size }
#=> ["World", "Hello", 3, 4, 5, 1, 2, 34, nil, nil, true, false]
This is because sort_by is not stable.
In order to fix it, we have to take their indices into account:
arry.sort_by.with_index { |e, i| [order.index(e.class) || order.size, i] }
#=> ["Hello", "World", 1, 2, 3, 4, 5, 34, nil, nil, true, false]
If you also want to match subclasses (e.g. make Integer match Fixnum in older Ruby versions), you have to pass a block to index:
order.index { |cls| e.is_a?(cls) }
I think this works:
arry.sort_by{ |v|
order.map{ |c|
v.is_a?(Object.const_get(c)) ? -1 : 1
}
}
# ["Hello", "World", 1, 2, 3, 4, 5, 34, nil, nil, true, false]
Object.const_get(c) converts the string to class.
Sorting, per se, is not necessary.
missing_classes = arry.map(&:class) - order
#=> [Symbol, FalseClass]
arry.group_by(&:class).values_at(*(order + missing_classes)).flatten
#=> ["Hello", "World", 1, 2, 3, 4, 5, 34, nil, nil, true, :cat, false]
I have been messing around with the .sort() and .concat() functions trying to combine multiple arrays of numbers and text into one and chronologically order it. It seems to work fine if only numbers are input into the array but as soon as there is text it seems to place it randomly, in this case between 6 and 7. Also, if I remove the number 3 from the array, the number 4 ends up out of order after the text, both being between 9 and 10. I have no idea why it is doing this if anyone could help.
With only numbers:
var multiArray = [[2, 8, 1, 7, 9], [6, 3, 5, 4, 10]];
// returns 1,10,2,3,4,5,6,7,8,9
document.write(multiArray[0]
.concat(multiArray[1])
.sort()
+"<br/>");
// returns 1,2,3,4,5,6,7,8,9,10
document.write(multiArray[0]
.concat(multiArray[1])
.sort((a,b)=>a-b)
+"<br/>");
and with text
var multiArray = [[2, 8, 1, 7, 9], [6, 3, 5, "text", 4, 10]];
// returns 1,2,3,4,5,6,text,7,8,9,10
document.write(multiArray[0]
.concat(multiArray[1])
.sort((a,b)=>a-b)
+"<br/>");
and without 3
var multiArray = [[2, 8, 1, 7, 9], [6, 5, "text", 4, 10]];
// returns 1,2,5,6,7,8,9,text,4,10
document.write(multiArray[0]
.concat(multiArray[1])
.sort((a,b)=>a-b)
+"<br/>");
first, combine all arrays in a single array
var multiArray = [[2, 8, 1, 'foo', 7, '1', 9], [6, 5, 'text', 4, 'bar', 10]];
var flattenArray = [].concat(...multiArray); // [ 2, 8, 1, 'foo', 7, '1', 9, 6, 5, 'text', 4, 'bar', 10 ]
then sort separately numbers and strings
const sortedNumbers = flattenArray
.filter(n => typeof n === 'number')
.sort((a, b) => a - b);
const sortedStrings = flattenArray
.filter(n => typeof n === 'string')
.sort();
finally, concat both
const sorted = sortedNumbers.concat(sortedStrings); // [ 1, 2, 4, 5, 6, 7, 8, 9, 10, '1', 'bar', 'foo', 'text' ]
I assume you want strings after your numbers. Otherwise, simply invert the concatenation:
const sorted = sortedStrings.concat(sortedNumbers);
I have multiple array (max 15), each can max have 1800 objects.
I need to combine them into a single array, so that I then can apply a delimiter (',') to generate a csv file. The problem is when I combine them into a single array, it has to be sequentially combined, like first objects of each array should be inserted initially followed by the 2nd index of objects then 3rd and so on.
I'm able to achieve the result I want by using a for-in loop. However this doesn't very swifty. I feel it can be done in a much cleaner way using functional methods available in swift (using map, reduce and filter functions).
However I'm not able to combine them perfectly. Can anyone help me with using the swift functional methods to achieve the result.
P.S: Let me know if you want me to post the for-in loop code, but I believe that's not required.
Given 4 (or more) arrays
let list0: [Int] = [ 1, 2, 3, 6, 7, 8, 9 ]
let list1: [Int] = [ 10, 20, 30, 40, 50, 60, 70, 80, 90]
let list2: [Int] = [ 100, 200, 300, 400, 500, 600, 700, 800, 900]
let list3: [Int] = [ 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000 ]
enumerate each one and put them into another array
let lists = [
list0.enumerate().map { (index: $0, array: 0, value: $1) },
list1.enumerate().map { (index: $0, array: 1, value: $1) },
list2.enumerate().map { (index: $0, array: 2, value: $1) },
list3.enumerate().map { (index: $0, array: 3, value: $1) }
]
Now you can write
let sorted = lists
.flatten()
.sort { ($0.index, $0.array) < ($1.index, $1.array) }
.map { $0.value }
[1, 10, 100, 1000, 2, 20, 200, 2000, 3, 30, 300, 3000, 6, 40, 400, 4000, 7, 50, 500, 5000, 8, 60, 600, 6000, 9, 70, 700, 7000, 80, 800, 8000, 90, 900, 9000]
I would consider making this an extension for arrays of arrays (although note you cannot do this directly, see this Q&A). You could then use a combination of reduce(_:_:) along with both flavours of flatMap(_:) in order to sequentially merge your arrays by iterating through the lengths of the inner collections and extracting the elements at each given index.
extension Array where Element : RandomAccessCollection, Element.Index == Int, Element.IndexDistance == Element.Index {
func joinedByTransposing() -> [Element.Iterator.Element] {
// The maximum length of the inner collections. Obviously if the 2D array is
// guaranteed to be n*m, you can optimise by just taking the first inner
// collection's count (and obviously you'll want to check that the array isn't empty first).
let maxIndex = self.reduce(0, {$0 > $1.count ? $0 : $1.count})
// Iterates through the max length of the inner collections, joining the restantant collections
// from the transform below into a single array.
return (0..<maxIndex).flatMap { index in
// Iterate through each inner collection, getting the element at the current index of iteration,
// or returning nil if the index is out of bounds. This flatMap will filter out any nils.
// If the 2D array is guarenteed to be n*m, this can be replaced by self.map { $0[index] }
self.flatMap { innerArray in
// Simple bounds check to get the element at the given index, or nil if out of bounds
index < innerArray.count ? innerArray[index] : nil
}
}
}
}
let array0 = [1, 2, 3, 4 ]
let array1 = [10, 20, 30 ]
let array2 = [100, 200, 300, 6, 7]
let result = [array0, array1, array2].joinedByTransposing()
print(result)
// [1, 10, 100, 2, 20, 200, 3, 30, 300, 4, 6, 7]
It’s worth noting that this solution has an overall time complexity of O(n * m) – whereas solutions that utilise sorted(by:) will have a time complexity of at least O(n * m * log(n * m)). For large arrays, this extra cost may well be non-trivial.
Here is my functional approach to the problem that you describe in your post.
First I flatted the array using the enumerated method, which return a tuple with the element position inside the array and It's value.
After this, You have an array with this tuples, next step, sort this big array by the offset (position) value of each element.
Once the array is sorted you hace to extract the value with map function.
And final step, once we have an array with sorted values, You have to reduce it to a string with the reduce function
// A group of arrays
var array1: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array2: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array3: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array4: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
// This is **your** array
let bigOne = [ array1, array2, array3, array4 ]
// And here is the functional concatenation.
let flattedOne = bigOne.flatMap({ $0.enumerated() }).sorted(by: { $0.0 < $0.1 }).map({$0.element}).reduce("")
{
return $0.isEmpty ? "\($1)" : "\($0), \($1)"
}
print(flattedOne)
Here's another approach...
public func sort(compound array: [[Int]]) -> [Int]
{
let max_index: Int = array.map({ $0.count }).max()!
var sorted: [Int] = [Int]()
(0 ..< max_index).forEach({ index in
array.forEach()
{
if $0.count > index
{
sorted.append($0[index])
}
}
})
return sorted
}
// A group of arrays
var array1: [Int] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 111, 112 ]
var array2: [Int] = [ 10, 20, 3, 4, 5, 6, 7, 8, 9, 10 ]
var array3: [Int] = [ 1000, 2000, 3, 4, 5, 600, 7, 8, 9, 10, 11 ]
var array4: [Int] = [ 100, 200, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
let bigOne: [[Int]] = [ array1, array2, array3, array4 ]
let sorted: [Int] = sort(compound: bigOne)
print(sorted)
And if you want the array as a CSV string...
print(sorted.reduce("") { return $0.isEmpty ? "\($1)" : "\($0), \($1)" })