This one is completed:
function combinePlayerOptions(playerOptions1 = {}, playerOptions2 = {}) {
const combined = Object.assign({}, playerOptions1, playerOptions2);
const keys = Object.keys(playerOptions1);
for (let i = 0; i < keys.length; i++) {
const prop = keys[i];
if (typeof playerOptions1[prop] === "object") {
combined[prop] = Object.assign(
{},
playerOptions1[prop],
playerOptions2[prop]
);
}
}
return combined;
}
function combinePlayerOptions(playerOptions1 = {}, playerOptions2 = {}) {
const combined = Object.assign({}, playerOptions1, playerOptions2);
Object.keys(playerOptions1).forEach(function checkObjects(prop) {
if (typeof playerOptions1[prop] === "object") {
combined[prop] = Object.assign(
{},
playerOptions1[prop],
playerOptions2[prop]
);
}
});
return combined;
}
This one is completed:
if (event.data === YT.PlayerState.PLAYING) {
players.filter(otherVideos).forEach(pauseVideo);
}
}
if (event.data === YT.PlayerState.PLAYING) {
for (let i = 0; i < players.length; i++) {
players[i].filter(otherVideos).pauseVideo();
}
}
This one is completed:
Object.entries(settings).forEach(function separate([param, value]) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
});
return options;
}
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;
}
eoja
August 22, 2021, 4:57am
2
Does this answer your question?
The Object.entries() method returns an array of a given
object's own enumerable string-keyed property
[key, value] pairs. This is the same as iterating
with a for...in loop, except that a
for...in loop enumerates properties in the prototype
...
I’m still having trouble doing this.
Yes, that’s part is right, what are you having a problem with? It would be helpful if you could show the code from inside the loop block, as the actual loop definition is fine
You can’t use a for...of
loop with objects, you need a for...in
loop. Here’s a simple example with a fake settings object:
const settings = {
mode: 'dark mode',
autoplay: 'false'
}
for (let param in settings) {
console.log('param is: ', param, '| settings[param] is: ', settings[param]);
}
If you convert the object to an array (which is what Object.entries()
does), you can use a for...of
loop:
for (let [param, value] of Object.entries(settings)) {
console.log('param is: ', param, '| value is: ', value)
}
Maybe I should have started with an easier one.
This is the type of for Loop
for (let i = 0; i < something here.length; i++) {
I wanted to convert this to:
Object.entries(settings).forEach(function separate([param, value]) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
});
return options;
}
I want to get better at doing these, I don’t know why they are so difficult for me to figure out.
These would be the simple for Loops.
Javascript For Loops in the ECMA Standard
Simple For Loop
ILM
August 22, 2021, 12:54pm
7
what have you tried to do?
Also the loop you presented before is a different kind of loop, a for loop and a for…of loop are not the same thing, they behave differently
I didn’t know that, I was looking at examples, and that was one of the ones I saw.
To convert this to a simple for Loop, what would I start with doing first?
Object.entries(settings).forEach(function separate([param, value]) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
});
return options;
}
I don’t get why you’re having a problem, you’ve already done it. in your original post. When you wrote “is this right?”, yes, it is , that’s how you do it. Where are you stuck??
Using a for…of loop is simpler than a for loop. I’m completely stumped as to why you aren’t trying to use the code you posted in the first post, but the exact equivalent would be
let settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
// so each one is [param, value], as you
// *already have*, you already done all
// this, the for loop is just more verbose
// param is
console.log(settingsEntries[i][0];
// value is
console.log(settingsEntries[i][1];
}
I almost got it. What did I do wrong here?
Where does this go? (function separate([param, value])
let settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
});
return options;
}
It doesn’t go anywhere, you’re already doing it. forEach runs some {thing} via a function for every item in the array. The loop runs {thing} for every item in the array
I got an error
https://jsfiddle.net/o5sw67pL/
Then I did this:
https://jsfiddle.net/o5sw67pL/2/
let settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
}
return options;
}
Working code with the forEach
https://jsfiddle.net/o5sw67pL/3/
What did I leave out or forget to do in the for Loop code?
I mean, delete the bracket it’s telling you to delete. You’ve have incorrect basic syntax, there’s a mismatch in the number of opening and closing brackets so the code can’t work
I did that here, then when the videos are clicked, they don’t open.
https://jsfiddle.net/o5sw67pL/2/
Working code with the forEach
https://jsfiddle.net/o5sw67pL/3/
Uncaught ReferenceError: param is not defined"
Does that have to do with this not being in it?
(function separate([param, value])
I can’t see all the code I’m afraid, so I don’t know where you’ve missed out something: jsfiddle doesn’t work on mobile. It’ll literally highlight the error in the editor though
I changed this:
let settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
});
return options;
}
To this:
let settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
}
return options;
}
After I did that I got this error:
Uncaught ReferenceError: param is not defined"
Original forEach code.
Object.entries(settings).forEach(function separate([param, value]) {
if (optionParams.includes(param)) {
options[param] = value;
} else {
options.playerVars[param] = value;
}
});
return options;
}
Well, no, you’ve decided to use a for loop so you now have no variable called param
, you have one called i
instead
const myArray = [1, 2]
How do I access the first value in this array?
// param is
(settingsEntries[i][0];
//value is
(settingsEntries[i][1];
I tried this:
That didn’t work.
let settingsEntries = Object.entries(settings);
for (let i = 0; i < settingsEntries.length; i++) {
if (optionParams.includes(param)) {
options[param] = value[i];
} else {
options.playerVars[param] = value[i];
}
}
return options;
}
ILM
August 23, 2021, 8:08am
19
and where is the code that determines this?
What’s param
? If you try to use variables, they have to be defined somewhere, JavaScript can’t just magically figure out what you mean.
Object.entries gives you an array like [[key, value], [key, value], [key, value]]
. How do you loop over an array?