4
I have a big problem, I have researched several places and I have not found the solution I hope you can help me... My goal is to create a JS function that will take an array as a base, which here I will call "To" his structure is this:
var A = {
a1: {
c1: 50,
c2: 50,
},
b1: {
d1: 0,
d2: [1 => "hello"],
d3: [2 => "world"],
}
};
The function will receive another array, which I will call "B", follows its structure:
var B = {
a1: {
c1: 1,
c2: 125,
},
};
After the array "B" go through the function it must "exit" with the same structure as the "To" and the values that were missing in the "B" identical to that of "To", that is to say:
Array "B" after passing the function:
var B = {
a1: { /* índice que já existia no array "B" */
c1: 1, /* índice e valor que já existia no array "B" */
c2: 125, /* índice e valor que já existia no array "B" */
},
b1: { /* índice que NÂO existia no array "B" e como o array "A" é a base foi criado esse índice no array "B" */
d1: 0, /* índice e valor que não existia no array "B", é a mesma história do b1 aqui em cima */
d2: [1 => "hello"], /* mesmo caso dos outros dois acima */
d3: [2 => "world"], /* mesmo caso dos outros três acima */
}
};
Let’s say so far so good, my big question is, I have the array "To" as a basis, when I want to modify the structure of "To" let’s say add one more level to it as in the example:
var A2 = {
a1: {
c1: 50,
c2: 50,
},
b1: {
d1: 0,
d2: [e1 => "hello"],
d3: [
f2 => "world",
f3 => [
g1 => "nova",
g2 => "camada"
]
],
}
};
The way I imagined so far this script would go wrong if I did that and it’s obvious I don’t want that to happen, what I really want is that array "B" when passing through the function, also receive the new layer...
I already started making this code but I can’t finish because of this damn question. Code I’ve already developed http://jsfiddle.net/mateusfmello/mja8ffs2/7/
Hello Matthew! I think you are calling Ray objects. This syntax
d2: [e1 => "hello"],
not Javascript. You meand2: {e1: "hello"},
?– Sergio
Exactly, I will correct... Thanks for the remark... You know how I can resolve this issue?
– MateusFMello
Now I came up with another question, what to use? arrays or objects? what you suggest?
– MateusFMello
For the example you have seems to me that objects is better. I will then take a look and already comment. One question: these objects have only strings and numbers inside them? or DOM elements and others?
– Sergio
It can have numbers, strings and objects... The big question is the objects... You got to take a look at the code that is in jsfiddle?
– MateusFMello
Yes, I saw jsFiddle. I’m going to answer (if nobody is faster than me) but I wanted to know more information to give the right answer.
– Sergio
Last question: you say "It can have numbers, strings and objects" I wanted to know if these objects have something from DOM?
– Sergio
It will not contain, but will result graphics in canvas... the data that will be passed to the function is the canvas data...
– MateusFMello