Add array elements to other arrays

Asked

Viewed 1,114 times

2

I want to add 3 elements to each empty preference array using the splice method on the condition that when data1 has 3 elements it jumps to data2 and adds 3 elements and when data2 has 3 elements it jumps to data3 and adds until arr.length is equal to 0. That’s what I’ve wanted to say since yesterday, but I’m new to this world. I know it’s not hard but I’ve tried and I couldn’t. A thousand apologies to all who have answered my question.

What I want is to create a function so I can add the elements to the 3 empty arrays at the same time. Exactly I do not want to copy the elements of the arr I want to take from an array and add to the empty arrays. for example take the first 3 indices and add in array1, take 3 indices and add in array2, take the last 3 indices and add in array3 but all at the same time.

i want to create a simple card game but with images so I have this array of images and the Divs with different class representing each player as I could not pass the images directly to the Divs I decided to create 4 arrays for each player but then tb had problem pk n was able to implement the loop. In the game I have to shuffle the cards, deal the cards, predict the moves, add up the value of the cards can I do with javascript? In your opinion what the best way to do?

var arr = [];

arr[0]='<img class="images" src="1.jpg">';      
arr[1]='<img class="images" src="2.jpg">';
arr[2]='<img class="images" src="3.jpg">';
arr[3]='<img class="images" src="4.jpg">';
arr[4]='<img class="images" src="5.jpg">';
arr[5]='<img class="images" src="6.jpg">';
arr[6]='<img class="images" src="7.jpg">';
arr[7]='<img class="images" src="8.jpg">';
arr[8]='<img class="images" src="9.jpg">';
arr[9]='<img class="images" src="10.jpg">';
arr[10]='<img class="images" src="11.jpg">';
arr[11]='<img class="images" src="12.jpg">';

The result would be this:

js

var data1 = ['elemento1','elemento2','elemento3'];
var data2 = ['elemento4','elemento5','elemento6'];
var data3 = ['elemento7','elemento8','elemento9'];
var data4 = ['elemento10','elemento11','elemento12'];

html

<div id="geral" >

    <div id="posi1" class="div_um"></div>   
    <div id="posi2" class="div_dois"></div> 
    <div id="posi3" class="div_tres"></div>
    <div id="posi4" class="div_quatro"></div>

</div>
  • 2

    Can you explain better what you want to do? It’s unclear. How so "I don’t want to make a copy"?

  • 3

    If you also explain the context we can help in the best way to do what you need.

  • what I want is to create a function so I can add the elements to the 3 empty arrays at the same time. Exactly I do not want to copy the elements of the arr I want to take from an array and add to the empty arrays. for example take the first 3 indices and add in array1, take 3 indices and add in array2, take the last 3 indices and add in array3 but all at the same time. theoretically I know how to do it but in practice I could not because I am asking for help from you.

  • magestik: I’d still like to leave one more answer but I’d like you to explain where these images come from (this is if the chicks are by hand or as the arr is filled) and also how you will use data1, data2 and data3. Knowing this I can leave a code/logic suggestion to use.

  • Sérgio I’m learning Javascript and I don’t know if I’ll explain it correctly pk there are terms I don’t know. But this is how these images come from .../images that will then be inserted into a div through data1, so data1 corresponds to a div, data2 corresponds to a div and data1 corresponds to a div. But I didn’t explain myself well so it’s hard to understand.

  • magestik: OK, and these Divs already exist in HTML? Can you put an example of the code? so you could loop over those images that you would distribute directly into each div. We just need to figure out what the code of these Divs is. If you have a specific class or a common relative.

  • I’ll explain what my purpose is with all this, i want to create a simple card game but with images so I have this array of images and the Divs with different class representing each player as I could not pass the images directly to the Divs I decided to create 4 arrays for each player but then tb had problem pk n was able to implement the loop. In the game I have to shuffle the cards, deal the cards, predict the moves, add up the value of the cards can I do with javascript? In your opinion what is the best way to do? I’m still getting started, Thank you.

  • Good! so I have the context I asked for. And do you already have any HTML I can use in an example in my answer? or are you starting from scratch? The other steps you have difficulty asking that we help.

  • Look you can use your html msm, I want in the examples comment the pk code interests me to understand the code

Show 4 more comments

2 answers

2

On the basis of their information data1, data2, data3 use only one array with all data's the method below can solve your problem:

        var arr = [];

        arr[0]='<img class="images" src="1.jpg">';      
        arr[1]='<img class="images" src="2.jpg">';
        arr[2]='<img class="images" src="3.jpg">';
        arr[3]='<img class="images" src="4.jpg">';
        arr[4]='<img class="images" src="5.jpg">';
        arr[5]='<img class="images" src="6.jpg">';
        arr[6]='<img class="images" src="7.jpg">';
        arr[7]='<img class="images" src="8.jpg">';
        arr[8]='<img class="images" src="9.jpg">'; 

        var dataX = [];

        for(var i = 0; i < arr.length; i+=3) {
            dataX.push(arr.slice(i, i+3));
        }

The result would be the variable dataX with the arrays of three elements:

inserir a descrição da imagem aqui

  • Thanks for the answer but I want to know how to add at the same time to in all empty pk array as far as shows I know how to do. what I want is to create a function so I can add to the 3 empty arrays at the same time.

  • @magestik always the number of elements to be placed in arrays will be multiples of three?

  • I want to add 3 elements at once.

  • @magestik changed the answer and that’s what you need?

  • Thanks for your help wasn’t what I wanted but I got it with the code I already had here.

  • 3

    Ok @magestik, if you believe that the solution would be useful to other users, post it as the answer to your question ;)

  • @williamhk2 got more information about the problem in the comments and added to the question. You might want to adapt the answer so it has 2 valid answers as help/reference...

  • @Sergio, ok. Today I’ll arrange the reply, thank you!

Show 3 more comments

1


A starting point could be this example I made: http://jsfiddle.net/s4Lbmgp9/

In the background it uses the Divs you mentioned, and within each iteration of these Divs you have a cycle for running 3 times. Every iteration of that for within the .each() it generates new HTML to be included in Divs.

In this example (that I mentioned above) he uses an Sprite that found here but if you already have background for the images you can ignore.

If you have an array of all the cards (eventually already drawn) then filling these 4 cards with these cards is even simpler. You can use it like this (a simplified version of the example above):

$('#geral > div').each(function (i) {
    var html = '';
    for (var j = 0; j < 3; j++) {
        html += '<div class="carta" style="background-position: ' + cartas[i + j] + '"></div>';
       // ou se tiveres HTML dentro dessa array simplesmente:
       // html += cartas[i + j];
    }
    this.innerHTML = html;
});

Live example: http://jsfiddle.net/2acv5m36/

Note that in the example I am using the positions in the array. You may have the url of the images in the array, then you have to make a small adaptation to my example. But take into account that an Sprite only loads once, making the page load much lighter.

  • good with your example already have mta thing to study and to begin now the doubt will be as I compare the value between two letters to know which one is worth more, if you know some example can indicate me

  • 2

    @magestik a good idea would be to do the whole game without thinking about the graphical part. That is, create the logic in the code with arrays and objects for each card. So you can ask him here about the steps you tried but can’t do it yourself. And at the end you put the graphic part together. If you’re not starting at the end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.