Did I do this right?
https://jsfiddle.net/ncg6v4oe/1/
var myStringArray = ["param", "value"];
let settingsEntries = Object.entries(settings);
var arrayLength = myStringArray.length;
for (let i = 0; i < settingsEntries.length; i++) {
for (i; i < myStringArray; i++) {
if (optionParams.includes(param)) {
options[param] = value[i][0];
} else {
options.playerVars[param] = value[i][1];
}
}
}
return options;
}
Hardcoding some string values isnāt the solution. param and values are variables that you need to extract from the settingsEntries.
Object.entries() turns an object into an array of arrays. Example:
const settingsObject = {
mode: 'dark',
autoplay: 'false'
}
Object.entries() will turn that into:
const settingsArray = [
['mode', 'dark'],
['autoplay', 'false']
]
⦠where the param you need is the first item in every subarray, and value is the second item.
In other words:
param = settingsEntries[i][0];
Iād recommend heavy console.logging so you see what is what.
Like this?
https://jsfiddle.net/kw4e26yu/
let myStringArray = ["param", "value"];
let settingsEntries = Object.entries(settings);
let arrayLength = myStringArray.length;
for (let i = 0; i < settingsEntries.length; i++) {
for (i; i < myStringArray; i++) {
if (optionParams.includes(param)) {
param = settingsEntries[i][0];
} else {
value = settingsEntries[i][i];
}
}
}
return options;
}
No, I think you might need to revisit basic JS loops.
The function Object.entries does this:
const exampleObj = {
firstName: "Dan",
secondName: "Couper",
}
const exampleObjEntries = Object.entries(exampleObj)
So exampleObjEntries is
[["firstName", "Dan"], ["secondName", "Couper"]]
Do you understand what it does, what it converts an object to?
So then if you loop over it, you just write a loop, using whatever type of loop you want: a for loop or a forā¦of loop or a while loop or an array method.
But if you donāt understand how to write loops, you may have an issue.
1 Like
Like this?
https://jsfiddle.net/xc5u9dzv/3/
const myStringArray = ["param", "value"];
const settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
for (i; i < myStringArray; i++) {
if (optionParams.includes(param)) {
options[param] = value[i][0];
} else {
options.playerVars[param] = value[i][1];
}
}
}
return options;
}
Full Code:
function separateOptions(settings) {
const options = {
playerVars: {}
};
const optionParams = ["width", "height", "videoid", "host"];
const myStringArray = ["param", "value"];
const settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
for (i; i < myStringArray; i++) {
if (optionParams.includes(param)) {
options[param] = value[i][0];
} else {
options.playerVars[param] = value[i][1];
}
}
}
return options;
}
function createPlayerOptions(settings) {
const options = separateOptions(settings);
const playerOptions = Object.assign({}, defaults, options);
playerOptions.playerVars = Object.assign({},
defaults.playerVars,
options.playerVars
);
return playerOptions;
}
Should I drop the inner loop, or give it itās own i var?
const myStringArray = ["param", "value"];
const settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
for (i; i < myStringArray; i++) {
if (optionParams.includes(param)) {
options[param] = value[i][0];
} else {
options.playerVars[param] = value[i][1];
}
}
}
return options;
}
Is this the correct way of doing it, or did I do it wrong?
From this:
Object.entries(settings).forEach(function separate([param, value]) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
});
return options;
}
To This:
const settingsEntries = Object.entries(settings);
function separate(param, value) {
for (let i = 0; i < settingsEntries.length; i++) {
if (optionParams.includes(param)) {
options[param] = value[i][0];
} else {
options.playerVars[param] = value[i][1];
}
}
}
return options;
}
Like this?
https://jsfiddle.net/9hkazfq8/1/
const entries = Object.entries(settings);
for (let i = 0; i < entries.length; i++) {
const [param, value] = entries[i];
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
}
return options;
}
ILM
August 23, 2021, 4:49pm
29
does one of the versions you posted work?
1 Like
Stuff like this makes me think you are missing some fundamental knowledge and skills. I think you would benefit greatly from reviewing core JavaScript:
1 Like
This one does, but just because it works, doesnāt mean it is written right.
const entries = Object.entries(settings);
for (let i = 0; i < entries.length; i++) {
const [param, value] = entries[i];
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
}
return options;
}
Iām working on converting this .forEach now.
if (event.data === YT.PlayerState.PLAYING) {
players.filter(otherVideos).forEach(pauseVideo);
}
}
My attempts
if (event.data === YT.PlayerState.PLAYING) {
for (let i = 0; i < players.length; i++) {
if (players[i].filter(otherVideos).pauseVideo();
}
}
I tried this:
if (event.data === YT.PlayerState.PLAYING) {
for (let i = 0; i < players.length; i++) {
if (players[i].filter(otherVideos))[i].pauseVideo();
}
}
Iām sorry, but I do not understand what is going on here. You are jumbling up what is original code and edited code in a way that makes it basically impossible for anyone to comment intelligently on what you are attempting to do.
Edit: While I was writing this, you edited yet again. Could you just write clearly
what you are attempting to do
what you are starting with
what you have so far
what you need help with
Iām working on now, converting this .forEach to for Loop
I finished the other code.
if (event.data === YT.PlayerState.PLAYING) {
players.filter(otherVideos).forEach(pauseVideo);
}
}
Iām working on, converting forEach to For loop.
I want to get better at it.
Ok, you seem unwilling to provide complete information. I am sorry, but I cannot help.
Iām starting out with this:
if (event.data === YT.PlayerState.PLAYING) {
players.filter(otherVideos).forEach(pauseVideo);
}
}
This was my attempt at converting it:
if (event.data === YT.PlayerState.PLAYING) {
for (let i = 0; i < players.length; i++) {
(players[i].filter(otherVideos)).pauseVideo();
}
}
I think this would be right:
From this .forEach
if (event.data === YT.PlayerState.PLAYING) {
players.filter(otherVideos).forEach(pauseVideo);
}
}
To this for Loop
if (event.data === YT.PlayerState.PLAYING) {
for (let i = 0; i < players.length; i++) {
players[i].filter(otherVideos).pauseVideo();
}
}
Butā¦the filter comes after , look:
players.filter(otherVideos).forEach(pauseVideo)
filter then loop over. Just to reiterate what has already been said, you donāt seem 100% comfortable with very basic concepts JavaScript like loops. All the functions (forEach) youāre trying to convert just run the function you give to them on a loop. At the very least you should try to stop guessing if youāve managed to do it, and actually check
2 Likes