I am using Javascript in an e-learning tool (Articulate Storyline)
I have created an array in this format:
var myarray = [
{ key: SliderName1, val: Slider1 },
{ key: SliderName2, val: Slider2 },
{ key: SliderName3, val: Slider3 }
]
having sorted the array, I then need to extract just ‘SliderName#’ of the element for the first, second and third elements as separate variables.
I am (very) new to JavaScript, and the closest I have got is:
var Top1 = myarray[0];
var Top2 = myarray[1];
var Top3 = myarray[2];
but I just require the “key”, not the whole element as a variable to return to the learning object.
Any assistance would be much appreciated.
ILM
January 29, 2021, 4:38pm
2
JamFestival:
var Top1 = myarray[0];
here you have extracted the object
how do you access an object property?
The syntax required by the software is
player.SetVar (">>variable in the software<<",>>variable in the JS<<);
So I have:
var Top1 = myarray[0];
Top1SL = Top1.toString();
player.SetVar ("Top3_1",Top1SL);
(Top3_1
being the variable to be written to in the software)
ILM
January 29, 2021, 4:43pm
4
in general, if you have an object, do you know how to access a specific property value?
like
var person = {
name: "Monique",
age: 28,
city: "Bergamo"
}
do you know how to get the value of the name
property?
No I don’t. I’m learning using Google and YouTube, and the instructions are always ‘console’-based, which is irrelevant to how I’m using it. I just need to use Javascript to ‘fill in the gaps’ of what the software can’t do natively; in this case, take variables and sort them.
This is the script, which currently returns [object Object] to the Articulate Storyline software:
var player = GetPlayer();
var Slider1 = player.GetVar("Slider1");
var Slider2 = player.GetVar("Slider2");
var Slider3 = player.GetVar("Slider3");
var Slider4 = player.GetVar("Slider4");
var Slider5 = player.GetVar("Slider5");
var Slider6 = player.GetVar("Slider6");
var Slider7 = player.GetVar("Slider7");
var Slider8 = player.GetVar("Slider8");
var Slider9 = player.GetVar("Slider9");
var Slider10 = player.GetVar("Slider10");
var Slider11 = player.GetVar("Slider11");
var Slider12 = player.GetVar("Slider12");
var Slider13 = player.GetVar("Slider13");
var Slider14 = player.GetVar("Slider14");
var Slider15 = player.GetVar("Slider15");
var Slider16 = player.GetVar("Slider16");
var Slider17 = player.GetVar("Slider17");
var Slider18 = player.GetVar("Slider18");
var SliderName1 = player.GetVar("SliderName_1");
var SliderName2 = player.GetVar("SliderName_2");
var SliderName3 = player.GetVar("SliderName_3");
var SliderName4 = player.GetVar("SliderName_4");
var SliderName5 = player.GetVar("SliderName_5");
var SliderName6 = player.GetVar("SliderName_6");
var SliderName7 = player.GetVar("SliderName_7");
var SliderName8 = player.GetVar("SliderName_8");
var SliderName9 = player.GetVar("SliderName_9");
var SliderName10 = player.GetVar("SliderName_10");
var SliderName11 = player.GetVar("SliderName_11");
var SliderName12 = player.GetVar("SliderName_12");
var SliderName13 = player.GetVar("SliderName_13");
var SliderName14 = player.GetVar("SliderName_14");
var SliderName15 = player.GetVar("SliderName_15");
var SliderName16 = player.GetVar("SliderName_16");
var SliderName17 = player.GetVar("SliderName_17");
var SliderName18 = player.GetVar("SliderName_18");
var myarray = [
{ key: SliderName1, val: Slider1 },
{ key: SliderName2, val: Slider2 },
{ key: SliderName3, val: Slider3 },
{ key: SliderName4, val: Slider4 },
{ key: SliderName5, val: Slider5 },
{ key: SliderName6, val: Slider6 },
{ key: SliderName7, val: Slider7 },
{ key: SliderName8, val: Slider8 },
{ key: SliderName9, val: Slider9 },
{ key: SliderName10, val: Slider10 },
{ key: SliderName11, val: Slider11 },
{ key: SliderName12, val: Slider12 },
{ key: SliderName13, val: Slider13 },
{ key: SliderName14, val: Slider14 },
{ key: SliderName15, val: Slider15 },
{ key: SliderName16, val: Slider16 },
{ key: SliderName17, val: Slider17 },
{ key: SliderName18, val: Slider18 }
]
myarray.sort(function(a, b){
return b.val - a.val;
});
var Top1 = myarray[0];
Top1SL = Top1.toString();
player.SetVar ("Top3_1",Top1SL);
ILM
January 29, 2021, 4:48pm
7
these three challenges in the fcc curriculum deal with that:
You might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn .
Happy coding
ILM
January 29, 2021, 4:50pm
8
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
1 Like
ILM
January 29, 2021, 4:52pm
9
that is what happens if you convert an object to a string, that’s why you need to access one of the object properties
Yes. The toString is there in the hope of resolving the issue, but like you say, I need to extract just one aspect of the element to make it work.
I’m assuming the line
var Top1 = myarray[0];
requires more added to it, or perhaps replacing with a slice, but the part I’m struggling with is creating a line(s) that says:
“Take the [first/second/third] element, extract just the ‘key’, and store it as a variable”
ILM
January 29, 2021, 5:07pm
11
if you learn how to access an object property, you can do it, myarray[0
] is an object, so you just need to access its key
property
Thanks for the advice. I’ve worked out I just needed a destructure line:
var Top1DestructuredLabel = Top1Object.key
It works now!!!
ILM
January 29, 2021, 8:21pm
13
that’s not destructuring, but good job in figuring out how to do it!
though you don’t need a new variable
JamFestival:
var Top1 = myarray[0];
you can just use dot notation here
system
Closed
July 31, 2021, 8:22am
14
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.