So let's say I have an array with a bunch of values say
"abc" = 5
"bcd" = 12
"ddd" = 13
I would like to be able to loop through and print all these out in a format similar to
abc: 5
bcd: 12
ddd: 13
If there is an assoc. array that's quite large and I don't know all the keys. How do I print out all of the keys and values?
Thanks
Try this:
for(var prop in obj) {
if(obj.hasOwnProperty(prop)){
console.log(prop + ': ' + obj[prop]);
}
}
Related
i have 2 arrays:
"interfaceTitles" with the values "USB ports", "digital inputs", "RS232, ...
"interfaceAmounts" with the values "3", "20", "1", ...
I need a merged Array with combined values. So the new array should have the following values:
"3 USB ports", "20 digital inputs" ...
So its not just concating, its fusionating :D
interfacesAdded = interfaceAmounts && interfaceTitles doesn't work
interfacesAdded = interfaceAmounts + interfaceTitles makes it into an string
"interfacesAdded" is declared as const interfacesAdded: any = ...
what can I do for this, Search function didn't help me, sorry im kinda unexperienced :(
Greetings
The below uses the map function to loop over the interfaceTitles list, and return a new list where the elements are the corresponding elements from the two lists joined by a space.
const result = interfaceTitles.map((_, i) => {
return interfaceAmounts[i] + " " + interfaceTitles[i];
});
Hope this is what you are looking for.
I think you could add the language you are using to help better answer,
the logic will be the same in different language, i'm doing it in js.
Admitting the string you want to concat are in the same position of the 2 arrays:
const array1 = [1, 4, 9, 16];
const array2 = ['a','b','c','d'];
// pass a function to map
// care in js we can use the + to concat string
// map will iterate over each element of array1
// x will be the current element of array1
// index will be the index of the current element,
// used to get the corresponding element in array2
const map1 = array1.map((x,index) => x+array2[index]);
console.log(map1);
// expected output: > Array ["1a", "4b", "9c", "16d"]
example
arr 1 = ['apple', 'blueberry', 'cherry']
aar 2 = ['green', 'blue', 'red']
function findAlpha (Number) {
?? i want to use arr1.indexOf
}
So, i want to return the colour green when 'apple' is logged.
I want the index in arr 1 to input the value of the same array in arr 2.
so cherry is index 2 and so is red. I want to use that to give red as the answer to cherry.
And an if else function, so that if a fruit is named that isnt in arr 1 it logs 'we don't have that info.'
You do not tell us which language you would work with. So, with javascript:
First, get the index of what you type in first array :
idx = arr1.indexOf(someWord);
second, if idx is equal to minus 1 say it is not good otherwise give the value of array 2 with the same index :
if (idx < 0)
{
console.log("We don't have info for that value");
}
else
{
console.log("value for", someValue, " is ", arr2[idx]);
}
Hope it helps.
This question already has an answer here:
Create multiple arrays in one line
(1 answer)
Closed 4 years ago.
I'm trying to get the method input to be stored and outputted like this:
Want [ ["email addresses"], ["phone numbers"], ["names"] ] - > [["bobsmith#example.com","sallyfield#example.com"],["555-555-5555","111-111-1111"],["Bob Smith","Sally Field"]]
This is my code:
def hash_2_array contacts
2 # Extract like info from each hash into arrays
3 stringArr = Array.new(3,Array.new(2)) #=> [ [ nil,nil] , [nil,nil] , [nil,nil] ]
4
5 if contacts.empty?
6 return nonsense = Array.new(3, Array.new)
7 else
8 n=0 #name counter
9 e=0 #email counter
10 p=0 #phone counter
11 contacts.each do |key, value|
12 stringArr[2][n] = key.to_s #adds name to array
13 n+=1
14 value.each do |key2, value2|
15 if key2.to_s.eql?("email")
16 stringArr[0][e] = value2.to_s #adds email address to array
17 e+=1
18 else
19 stringArr[1][p] = value2.to_s #adds phone number to array
20 p+=1
21 end
22 end
23 end
24 end
25 return stringArr
26 end
27
28
29 hash_2_array({:"Bob Smith"=>{:email=>"bobsmith#example.com", :phone=>"555-555-5555"}, :"Sally Field"=>{:email=>"sallyfield#example.com", :phone=>"111-111-1111"}})
It returns:
got: [["555-555-5555", "111-111-1111"], ["555-555-5555", "111-111-1111"], ["555-555-5555", "111-111-1111"]]
It's really confusing why it's not just assigning only the the index in the array that I'm specifying. I think this code worked before, but now it's broken somehow. Any help would be great
From the fine manual:
new(size=0, default=nil)
new(array)
new(size) {|index| block }
[...]
Common gotchas
When sending the second parameter, the same object will be used as the value for all the array elements:
a = Array.new(2, Hash.new)
# => [{}, {}]
a[0]['cat'] = 'feline'
a # => [{"cat"=>"feline"}, {"cat"=>"feline"}]
a[1]['cat'] = 'Felix'
a # => [{"cat"=>"Felix"}, {"cat"=>"Felix"}]
If multiple copies are what you want, you should use the block version which uses the result of that block each time an element of the array needs to be initialized:
a = Array.new(2) { Hash.new }
a[0]['cat'] = 'feline'
a # => [{"cat"=>"feline"}, {}]
When you say this:
stringArr = Array.new(3,Array.new(2))
You're creating an array with three elements but all those elements are exactly the same array. You want an array that contains three different arrays as elements:
stringArr = Array.new(3) { Array.new(2) }
im tryin to sort a two-dimensional Array in Ruby by the first value, like that:
files_array = Array.new(2) {Array.new}
files_array[0][0] = 42
files_array[1][0] = "/media/js/aefc015sdfsdf0728175535.js42"
files_array[0][1] = 21
files_array[1][1] = "/media/js/aefc015sdfsdf0728175535.js21"
files_array[0][2] = 30
files_array[1][2] = "/media/js/aefc015sdfsdf0728175535.js30"
i tried
files_array.sort!{|a,b| b[0] <=> a[0]}
but it returns:
`sort!': comparison of Array with Array failed (ArgumentError)
This is how i want the array to be sorted:
files_array[0][0] = 21
files_array[1][0] = "/media/js/aefc015sdfsdf0728175535.js21"
files_array[0][1] = 30
files_array[1][1] = "/media/js/aefc015sdfsdf0728175535.js30"
files_array[0][2] = 42
files_array[1][2] = "/media/js/aefc015sdfsdf0728175535.js42"
`sort!': comparison of Array with Array failed (ArgumentError)
This is because a[0] and b[0] are elements of an array of arrays, i.e. they are arrays themselves.
If you did this:
files_array.sort!{|a,b| b[0][0] <=> a[0][0]}
It would work.
Incidentally, this looks like a great example of when an associative array would be a better idea than an array of arrays.
files_hash = {
42 => "/media/js/aefc015sdfsdf0728175535.js42",
21 => "/media/js/aefc015sdfsdf0728175535.js21",
30 => "/media/js/aefc015sdfsdf0728175535.js30"
}
Not only is this a lot clearer in code, but you do away with the need to maintain a sorted 2-dimensional array.
try this it will work for you:
files_array = [[42,"/media/js/aefc015sdfsdf0728175535.js42"],[21,"/media/js/aefc015sdfsdf0728175535.js21"],[30,"/media/js/aefc015sdfsdf0728175535.js30"]]
sorted = files_array.sort { |x,y| x[0] <=> y[0] }
puts sorted
Result:
21
/media/js/aefc015sdfsdf0728175535.js21
30
/media/js/aefc015sdfsdf0728175535.js30
42
/media/js/aefc015sdfsdf0728175535.js42
I have this 2D array.
[(476301.98163511883, 6176897.129456658)
(476723.365551495, 6176895.078376785)
(477124.59457628336, 6176893.28525448)
(477525.82249430567, 6176891.306532074)
(477927.0510582989, 6176889.4760845825)
(477925.0121537624, 6176487.379134962)
(477922.97333802003, 6176085.2824224755)
(477920.93404681794, 6175683.074655607)
(477918.79328165855, 6175260.834659822)]
I'm trying to add 10 to "X" the first column and 20 to the "Y" column. I can't figure out how to access each column while keeping the array structure as is.
I can do something like this
x = array['X'] + 10
y = array['Y'] + 20
However now the array is split and a need the x, y pair together like in the original array. Thanks
I guess your columns are named 'X' and 'Y', and that array is a numpy.array?
In that case you can edit inplace by using the += operator:
array['X'] += 10
array['Y'] += 20
or, if they are not named
array[:, 0] += 10
array[:, 1] += 20
This is the same as
array['X'] = array['X'] + 10