I am trying to create a variable Youtube Object that will be simulated in a for loop.
The concept is that I want to set a variable speed to each clip in a for loop. The idea is that YouTube takes in an object as described here. This seems difficult.
I created the function here:
function YTObjects(videoId1, ...args) {
let object1 = {
height: "315",
width: "560",
videoId: videoId1,
playerVars: {
autoplay: 0,
loop: 1,
...(typeof args[0] !== "undefined" && { start: `${args[0]}` }),
...(typeof args[1] !== "undefined" && { end: `${args[1]}` }),
},
if ((typeof args[2] !== "undefined") && (typeof args[3] !== "undefined")) {
event: function (args[2], args[3]) {
player.setPlaybackRate(args[2]*args[3]);
},
}
};
return object1;
}
Which enables me to create a variable object. But I am failing to create a function in a key that depends on the arguments (2&3).
Can you please help in this regard?
Either use a ternary operator to conditionally assign the event property, or use an if after the object is defined. You probably don't want any params in the anonymous function that's being added.
let object1 = {
height: "315",
width: "560",
videoId: videoId1,
playerVars: {
autoplay: 0,
loop: 1,
...(typeof args[0] !== "undefined" && { start: `${args[0]}` }),
...(typeof args[1] !== "undefined" && { end: `${args[1]}` }),
},
event: ((typeof args[2] !== "undefined") && (typeof args[3] !== "undefined"))
? function () {
player.setPlaybackRate(args[2]*args[3]);
},
: undefined
};
return object1;
or
let object1 = {
height: "315",
width: "560",
videoId: videoId1,
playerVars: {
autoplay: 0,
loop: 1,
...(typeof args[0] !== "undefined" && { start: `${args[0]}` }),
...(typeof args[1] !== "undefined" && { end: `${args[1]}` }),
}
};
if ((typeof args[2] !== "undefined") && (typeof args[3] !== "undefined")) {
object1.event = function () {
player.setPlaybackRate(args[2]*args[3]);
};
}
return object1;