2
I have 2 arrays:
array1 = [
...
{
"id_projeto": 4,
"sigla": "ADM-APR",
"nome": "Administração APR",
"descricao": null,
"atividades": [ 13, 14, 49, 80, 81, 82 ] // id
}
...
];
array2 = [
...
}
"id_atividade": 49, //id
"codigo": "P46",
"nome": "Treinamento de Instalação e Manutenção",
"data_inicio": "2017-04-01",
"data_fim": "2017-04-30",
"prioridade": "1 ",
},
}
"id_atividade": 50, //id
"codigo": "P47",
"nome": "Manutenção das estações",
"data_inicio": "2017-04-01",
"data_fim": "2017-04-30",
"prioridade": "1 ",
}
...
];
I have to create a new array with the activities(array2) of each project(array1), it would look like this:
array3 = [
...
{
"sigla": "ADM-APR",
"nome_projeto": "Administração APR",
"descricao": null,
"id_atividades": [ 13, 14, 49, 80, 81, 82 ],
"atividades": [
{
"id_atividade": 49,
"codigo": "P46",
"nome": "Treinamento de Instalação e Manutenção",
"data_inicio": "2017-04-01",
"data_fim": "2017-04-30",
"prioridade": "1 ",
},
...
]
}
...
];
array3 = array1.atividades.map(obj => {
array2(ativ => {
if(obj == ativ.id_atividade){
return console.log(ativ)
}
})
});
When returning the result in the console.log, the answer is the expected one, when returning the answer the array receives the correct number of activities, but, Undefined.
I’m trying to do this within a mutations in vuex, I haven’t succeeded yet, but it has given me some idea. Vlw ! Do you have any tips for article, tutorial, video, anything that helps me work better with javascript arrays ? I always have trouble with that.
– user90136
@mtAlves if you’re using Vue.js/Vuex explains the question better. Shows how the
actions
and themutations
and what you want to do. If each ID is searched by ajax the corresponding element you should do inactions
for themutations
shall only have synchronous code.– Sergio
Actually I’m starting to use vuex now and I started with web development will be 3 months, so it’s all kind of messy and I’m still kind of lost kk, but give a look at github.com/mtAlves/Pta
– user90136
@mtAlves I use Vue at work and I think it’s great. I don’t think this question will make you understand everything. Is the app great? Do you really need Vuex? It might be easier to start without Vuex...
– Sergio
An average app, but maybe it’ll get big. I’m not sure if I need Vuex, but as I’m trying to learn, I found it interesting to start using it. And yes, Vue is excellent, I had never written a line of css 3 months ago and I can already develop something, even if giving headbutt kkk
– user90136
@mtAlves ok. So I suggest you ask here step by step, there are more people here who also know about Vue.js and can help. Regarding the question, say what you could not use/understand to get this problem solved.
– Sergio
I did, but the.log console prints the expected result, but the data is Undefined. I will edit the question to better view.
– user90136
@mtAlves in this case you have an asynchronous condition that you are not respecting.
– Sergio
@mtAlves have just seen your Edit. You are not using my code. Put here a jsFiddle with all that code that I can take a look at and fix
– Sergio
I couldn’t do it in jsFiddle, if I could just look at the code later on github (https://github.com/mtAlves/pta/blob/master/src/store/mutations.js), I went up the fake file using json-server tb (server.json). I’ll get some sleep. But thanks for the force anyway.
– user90136
@mtAlves then in case there is no ID you want to return
''
or{}
. Return{}
or remove from the array seems more correct than return''
– Sergio
I did in pythontutor https://goo.gl/Z3oUFW even looking frame by frame I can’t understand pq return Undefined, I expected to return the whole object.
– user90136
@mtAlves saw my last question here?
– Sergio
@mtAlves edited the answer with a new example, I used an empty object when not finding
– Sergio
Funcionouuuuuuuuu, I think I love you man. I hadn’t thought about find(). Thank you really!!
– user90136
@mtAlves great. I had used the
find
in the first example I had given, but now with the second example perhaps it has become clearer. I am glad to help :)– Sergio