How to use d3.max() with an associative array [duplicate] - d3.js

This question already has answers here:
How to use d3.min and d3.max within a d3.json command
(3 answers)
Closed 8 years ago.
If I want the maximum value of an array, I can do this:
var data = [1,2,3,4,5];
console.log(d3.max(data)); //output 5
But if I try with an associative array, I get 'undefined':
var data = {'foo' : 4, 'baz' : 8};
console.log(d3.max(data)); //output undefined
How do I use d3.max() with an associative array?

Based on help from the comments to the question, this was the solution:
d3.max(d3.values(data));

Related

Is there any way to write two values in one name in python [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
f = open("dict.txt","a")
f.write("Customer_Name: "+Name+"\nMovies: "+Movies_Name+"\nQuantity: "+str(quantity)+"\n")
f.close()
Movies_Name is a list which might contain more than one movies. Now I want to write the names of the movies individually instead of writing them as a list.
For me it has nothing to do with discord itself. It is just Python question.
So answer can be found here
By using ''.join
list1 = ['1', '2', '3']
str1 = ''.join(list1)
In your case:
f.write("Customer_Name: "+Name+"\nMovies: "+''.join(Movies_Name)+"\nQuantity: "+str(quantity)+"\n")

Ruby what does star prefix means in a loop variable? [duplicate]

This question already has answers here:
What does the (unary) * operator do in this Ruby code?
(3 answers)
Closed 5 years ago.
I've ecountered this today and I have no idea what it means. I tried to google it but I had no luck. Can someone explain this to me?
combinations.each do |combination|
messages = EventNotification.where('user_id = ? AND message_template = ?', *combination)
...
end
It's called the splat operator, and it unpacks an array into single method arguments. In this case, because the function presumably expects two more arguments after the format string, it's equivalent to:
messages = EventNotification.where('user_id = ? AND message_template = ?',
combination[0], combination[1])
In other languages, this feature is often called "varargs".

Iterate over array of hash [duplicate]

This question already has answers here:
How to sort an array of hashes in ruby
(5 answers)
Closed 6 years ago.
I have a array of hash something like
[{a1: 'a', b1: 'b'},{a1: 'c', b1: 'b'},{a1: 's', b1: 'cq'}]
I want to sort the array in the ascending value of b1 and return an array. Sorry if its easy but got confuse with the logic.
Following will return you an array sorted based on b1
array_of_hashes.sort_by { |hsh| hsh[:b1] }

ruby 'best way' to generate 2d array [duplicate]

This question already has answers here:
How to declare an empty 2-dimensional array in Ruby?
(5 answers)
Closed 6 years ago.
I have create empty array of array like below , Is there any other best way to initialize it ?.
arr = [[],[],[],[],[],[],[],[],[]]
I think the best way to achieve it by using Array class.
ex:
Array.new(width){Array.new(height)}
you can also provide width & height value like width = 2 & height = 4
Have you tried something like
arr = Array.new(9) { Array.new }
?

Finding the index of a particular element in an array with duplicated elements [duplicate]

This question already has answers here:
How can I find out the position of an item contained by an array?
(3 answers)
Closed 9 years ago.
Let's say I have the following array:
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
I want to find the index of the first "b" in the array. What is the best way of doing it?
Use Array#index
for first occurrence and Array#rindex for last occurrence of an element.
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
array.index("b") # => 6
array.rindex("b") # => 11

Resources