If I have the array:
my_array = [{a:3,b:4,c:7},{a:5,b:8,c:6}]
property_names = ["a","c"]
How can use the property_names array against my_array to get the following output?:
subset_array = [[3,7],[5,6]]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This will give you
[[3, 7], [5, 6]]and seems to be just what you want.[Obsolete initial answer; before the OP updated his question]
Use
o.aando.c– no magic involved and no kittens harmed!You could also use the
[]syntax:o['a']ando['c']– but usually you only use it if you want to access a dynamic property name (i.e. a variable instead of a quotes string inside the[])If you want an array with both values:
Now you can use
a_and_c[0]and[1]to access those two values.