How to select a specific record from an array

Asked

Viewed 84 times

1

I’m having the following problem. I dynamically created an array to receive some information. selections [2]{"2","alt-2-2"} [7]{"3","alt-7-3"} [8]{"1","alt-8-1"}

Well, in the key I write the id in the database of this record, then it is recorded the option chosen between 1 and 3 and in the last one the id of the div that was chosen. You may notice that the id of div is composed of "alt-id_no_mysql-option-chosen". Only now let’s say only option 3 is correct, only key 7 is right, and the rest are wrong. How do I navigate in the array searching which one is correct? I know that if the keys were sequential like 0,1,2... one is already solved, as I do in this case then?

  • I think this is what you want: var value = myArray.find(x => x == 'alt-2-2');

  • @Marcelovieira this find() function returns only the one that is true. But I can have dozens of true ones inside this array

  • But you want to return all that has the chosen option is this ? That is if you have an "alt-7-3" and another "alt-1-3" and it is the option "3" you have to return the 2 ?

  • @Isac exactly

  • This one of yours selections is an array or object ? What’s inside is object or array ? If it’s object what are the keys ? It’s like an example of your selections ?

  • "selections" is an array. Hence I need to record what was selected. As the change can be performed before saving I did so: selections[id_no_db] = opcao_selecionada; selections[id_no_db] = id_da_div;

  • Hence if there are changes it will always overwrite in the id key. Only that these id vary depending on the situation.

  • and what is within each position of selections ? {"2","alt-2-2"} is not a valid object. You can display a console.log(selections) ?

  • (7) [Empty 2, Array(1), Array(1), Array(1), Array(1), Array(1)]2: ["2", index: 4, input: "alt-2-2", groups: Undefined]3: ["1", index: 4, input: "alt-1-3", groups: Undefined]4: ["1", index: 4, input: "alt-1-4", groups: Undefined]5: ["2", index: 4, input: "alt-2-5", groups: Undefined]6: ["3", index: 4, input: "alt-3-6", groups: Undefined]length: 7__proto__: Array(0) temp1 (7) [Empty 2, Array(1), Array(1), Array(1), Array(1), Array(1)]

  • I still can’t figure out exactly what’s in each position, which seems to be specific objects of a class you have. selections[2].input gives you "alt-2-2" ? How was this built selections ? If I can put an example of how you built this, I can put the answer with a working example, otherwise it gets harder.

  • I just declared it. var selections = [] ; Then I already use it by casting id and Selected as I reported a little above

  • @Alessandrocarvalho can paste here the code piece that makes the release? let’s say selections[1] = objetoQueEstaInserindo; What I wanted to know more precisely would be the return of console.log(objetoQueEstaInserindo);

Show 7 more comments
No answers

Browser other questions tagged

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