Append entire jQuery object to html - javascript

I want to clone some players inside my .shuffler div. Because I attached click-listeners to the .player divs I want to append the entire object instead of just their HTML.
When I loop through the code below I do get the correct result when logging playerClones[j] in the each loop but it doesn't append anything.
Any ideas on how to clone my players and append every player 25 times to my .shuffler. without losing the click listener?
this.amountOfDuplicates = 25;
var playerClones = [];
this.parentObject.find('.player').each(function () {
playerClones.push(jQuery(this).clone());
});
for (var i = 0; i < this.amountOfDuplicates; i++) {
for (var j = 0; j < playerClones.length; j++) {
this.parentObject.find('.shuffler').append(playerClones[j]);
}
}
Fiddle here!

Normally when you use $(this).clone(), everything of the object is cloned except the click listener.
It is better if you could declare a function like this and call it every time u clone the object.
attachClickToClone = function() {
$('.player').on('click', function() {
//to-do-function
}
}
And call it like this.
for (var i = 0; i < this.amountOfDuplicates; i++) {
for (var j = 0; j < playerClones.length; j++) {
this.parentObject.find('.shuffler').append(playerClones[j]);
}
}
attachClickToClone();

Related

Append img elements if one doesnt exist

Im running a function that create img elements. I only want it to create the element if and identical element doesnt already exists. There are 2 copies of 6 diffrent cards in the cardArray, and right now the function create copies of previously found cards.
Is there any easy way to do this?
function wonCards() {
for (let j = 0; j < cardsWonMerged.length; j++) {
for (let i = 0; i < cardArray.length; i++) {
if (cardsWonMerged[j] == cardArray[i].name) {
const card = document.createElement('img')
card.setAttribute('src', cardArray[i].img)
card.setAttribute('dataTwo-id', i)
card.setAttribute('name-id', cardArray[i].name)
gridTwo.append(card)
}
}
}
}

How to push items into a dynamically created array

So I'm just beginning to code and have learnt only basic codes and I have created arrays dynamically with:
for (var j=0; j<20; j++) {
this["row"+j] = [];
for (var i=0; i<10; i++) {
["row"+j].push("false");
}
}
but now I want to make it so that I can push items into the previously created row1[], row2[], etc... so that it could be like row1[false, false...] and row2[false, false...] Is there a way to do that?
It is not clear what this is referring here. Rather I will suggest to create an object and in that object keys will be ["row" + j], the value of which will be an [] and then inside the nested loop push value to that array
let obj = {};
for (var j = 0; j < 20; j++) {
obj["row" + j] = [];
for (var i = 0; i < 10; i++) {
obj["row" + j].push("false");
}
}
console.log(obj)
If you want to create arrays not inside object but like normal arrays you can use window and then variable name to create variable.
Normally when you declared varibale in will be inside window object(you can access it without writing window.{variable name}). So you can take advantage of window to created dynamic variable.
for (var j = 0; j < 20; j++) {
window["row" + j] = []; //<-----use window to create dynamic array
for (var i = 0; i < 10; i++) {
window["row" + j].push("false");
}
}
row1[0] = "true"; // editing content of array
console.log(row1,row2);
In the above we have created 20 variable name row0,row1,.... and you can directly access it like normal arrays.

Find if Array contains a user

I have a parse background job that contains a simple query.each for one class. This class has 2 Arrays field filled with objects.IDs. Inside this query, for every single object, i need to check if the objects.ID of the first Array are contained in the second Array.
Basically in a simple loop:
var j = 0;
for (var j = 0; j < firstArray.length; j++) {
if(firstArray[j] "isContainedIn" secondArray){
// my custom code
}
}
What i can't figure out is the function to use, if exist..Does javascript have a function like that or i need to make a nested loop to achieve my goal?
EDIT: i worked it out using indexOf but the solution proposed by Shqiptar didn't work so here is the one that actually works:
first Array name = usersEligibleToVote
second Array name = usersThatVoted
for (var j = 0; j < usersEligibleToVote.length; j++) {
if(usersThatVoted.indexOf(usersEligibleToVote[j]) === -1){
console.log("user.id "+usersEligibleToVote[j]+" needs to vote");
} else {
console.log("user.id "+usersEligibleToVote[j]+" has voted");
}
}
var j = 0;
for (var j = 0; j < firstArray.length; j++) {
if(firstArray[j].contains(secondArray))
{
// your custom code here
}
}
And then for checking if an object is the same :
var j = 0;
for (var j = 0; j < firstArray.length; j++) {
if(firstArray[j].indexOf(secondArray) != -1)
{
// your custom code here
}
}
I would process it through a simple jQuery and javascript for loop like so:
var arr1;
var arr2;
var i;
for(i = 0; i < arr1.length; i++) {
if ($.inArray(arr1[i], arr2) {
break; //do things here or what not
}
}

Nested array for loop

I'm stuck trying to figure out how to loop trough nested arrays.
I have an array called worldShapes that contains child arrays. I want to loop trough the parent array and get all the child arrays from it.
Here's my attempt:
//Nested array
worldShapes = [
[33,108,66,141,99,174,99,207,132,207,165,207,165,240],
[132,306,165,306,165,339,165,372,132,405,99,405,99,438,132,438,165,438],
[198,339,231,339,264,372,297,372,330,405,363,438,396,438],
[198,174,198,273,231,306,264,306],
[231,174,231,240,264,273,297,273],
[396,306,462,306,495,339,495,372,528,405,528,438,561,438,594,471],
[660,504,561,504,495,504]
];
//trying to loop trough each item in the child array
(function(){
var wShapes = worldShapes; //create a local variable
var wLen = wShapes.length; //store the length as a variable
for (var i = 0; i < wLen; i++) {
for (var j = 0; j < wShapes[i].length; j++){
console.log(wShapes[i][j]); //this is propably wrong, trying to access the current child item of the current parent array
}
}
})
Just add (); to the very end of your code ;-)
You've simply forgotten to invoke your anonymous function
To execute the function, add ()
(function () {
var wShapes = worldShapes; //create a local variable
var wLen = wShapes.length; //store the length as a variable
for (var i = 0; i < wLen; i++) {
for (var j = 0; j < wShapes[i].length; j++) {
console.log(wShapes[i][j]); //this is propably wrong, trying to access the current child item of the current parent array
}
}
}()); // here
Fiddle

Remove all occurrences of property

I have code like this
for (var j=0;j<100;j++){
...
data[j].property1 = something;
}
and now I want to remove all ocurences of property1. something like this
remove data[]['property1']
is there easy way to do that, or must I ensure it by cycle?
There is no way to do it in 1 step.
You must do it in a loop:
function remove_property(arr, property_name)
{
for (var i = 0; i < arr.length; i++) {
delete arr[i][property_name];
}
}
remove_property(data, 'property1');
Or maybe you can put your property in another array and then delete this other array directly.
var property1 = [];
for (var i = 0; i < data.length; i++) {
property1[i] = something;
}
...
delete property1; // 1 step

Categories

Resources