Working with multiple javascript/jquery arrays

Asked

Viewed 102 times

0

Speak person, basically I’m getting 2 array per function parameters and that both NOT have the same size, and I’m trying to render it in each specific select, for example: The 1st array comes an API with the information of the selects (value, name...) and it is updated every time I select a multiselect... - I will call it array-API1.

The 2nd array comes with a collection of numbers that are exactly what selects this information should go to - I’ll call it array-target

So for example:

array-API1 = [{val: 1, name: 'Section 1'}, {val: 2, name: 'Section 2'}] 
array-API1 = [{val: 3, name: 'Section 3'}, {val: 4, name: 'Section 4'}, {val: 5, name: 'Select 5'}]    

array-target = [4, 7]

I need to play the array-API1 items {val: 1, name: 'Section 1'}, {val: 2, name: 'Section 2'} in the id="select-4", which is the first number of the array-target, of course concatenating... $('"select-"+array-target[0]')

and then play the new array-API1 items val: 3, name: 'Section 3'}, {val: 4, name: 'Section 4'}, {val: 5, name: 'Select 5'} in the id="select-7" which is the second number of the array-target...

function appendSelect( array-API1, array-target ) { 
    array-API1.forEach(function (item, index) {
        option = $('<option>');
        option.val(item.section);
        option.html(item.name);

        /*Aqui no appendTo que to sofrendo provavelmente... pois quero de 
        alguma forma jogar dentro do select certo, porque aqui não tenho 
        acesso ao array-target, até tenho, só que só pensei fazendo um forEach 
        dentro do outro, só que não deu certo, mas grossaramente eu queria 
        fazer um append assim, mas de forma dinâmica, desse jeito só faço pro 
        #select-4*/

        option.appendTo("'#select-'+array-target[0]"));
    });
}
  • I did not understand the mechanics. As you will use two different objects at the same time with the same name?

  • The API1 array is only updated, so I did the simulation, seeing 2 different collections, for each array it will do a foreach and build the <option> to play in select (target) The problem is that I can’t use these 2 array inside the foreach... the array-API1 and the array-target

  • I still can’t understand mt well, but if you remove the first item from the array-target, the next time you call the function, the code array-target[0] will take the "7"?

  • That’s basically what I want Hahaha... I’ve done it, but always getting the last one with . pop() but the problem is that it is a multiselect, so if one selects with the multiple shift, it will only return the first or the last... [1, 2, 3, 4, 5, 6, 7, 8]. pop() will return only 8 I wanted a way to do the following array1 = [1, 2] array2 = [1, 2, 3, 4] arrayFinal = [3, 4] (the Final array would only receive new items, which does not exist in array1)

  • I thought putting array-target.shift() after append to remove the first item from the array...

No answers

Browser other questions tagged

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