0

ive searched here and i cant find a solution for this, i think im just missing some syntax.

var DataSend = [];
for ( $i=0; $i < $MovimentosLength; $i++) {
    DataSend = [
        $i =[
            'CodProduto', CacheQL.Movimentos.CodProduto[$i],
            'ProdutoDesignacao', CacheQL.Movimentos.ProdutoDesignacao[$i],
            'Valor', CacheQL.Movimentos.Valor[$i],
            'Percentagem', CacheQL.Movimentos.Percentagem[$i]
        ],
    ]

}//#FOR

is this possible? I know how to do it in PHP but in js i dont really know the syntax.

Thanks

4
  • You are overwriting that $i variable with every loop. Commented Jul 4, 2019 at 13:08
  • DataSend[$i] = ['CodProduto',...] Commented Jul 4, 2019 at 13:08
  • stackoverflow.com/questions/966225/… Commented Jul 4, 2019 at 13:08
  • JavaScript has modernised iteration commands like for...of and functional utilities like Array.prototype.map and Array.prototype.reduce that could probably make this easier to read/write. If you show more code around this problem, we may be able to give you a better solution. Commented Jul 4, 2019 at 13:39

3 Answers 3

2

You're almost there! It's just that with javascript there's no shorthand for pushing to an array. You have to use push().

Also from your example it looks like it's not a 2D array, but an array of objects. Something like this:

const DataSend = [];
for (let i=0; i < MovimentosLength; i++)
{
    DataSend.push({
                CodProduto: CacheQL.Movimentos.CodProduto[i],
                ProdutoDesignacao: CacheQL.Movimentos.ProdutoDesignacao[i],
                Valor: CacheQL.Movimentos.Valor[i],
                Percentagem: CacheQL.Movimentos.Percentagem[i]
            });
}

Does that make sense?

Sign up to request clarification or add additional context in comments.

Comments

0

Actually it's pretty similar though I don't know what the source CacheQL.Movimentos in your case is. An array too?

Initialize a new array

var DataSend=[];

loop over the object

for(var a=0;a<CacheQL.Movimentos.length;a++)
{
}

and inside the for loop push a new array to the existing

DataSend.push(['CodProduto', CacheQL.Movimentos.CodProduto[a],
                    'ProdutoDesignacao', CacheQL.Movimentos.ProdutoDesignacao[a],
                    'Valor', CacheQL.Movimentos.Valor[a],
                    'Percentagem', CacheQL.Movimentos.Percentagem[a]);

Comments

0

Just use push with a valid object literal:

for (let i = 0; i < MovimentosLength; i++) {
  DataSend.push({
    CodProduto: CacheQL.Movimentos.CodProduto[i],
    ProdutoDesignaco: CacheQL.Movimentos.ProdutoDesignaco[i],
    Valor: CacheQL.Movimentos.Valor[i],
    Percentagem: CacheQL.Movimentos.Percentagem[i]
  });
}

Or if you wanted an array:

for (let i = 0; i < MovimentosLength; i++) {
  DataSend.push([
    "CodProduto", CacheQL.Movimentos.CodProduto[i],
    "ProdutoDesignaco", CacheQL.Movimentos.ProdutoDesignaco[i],
    "Valor", CacheQL.Movimentos.Valor[i],
    "Percentagem", CacheQL.Movimentos.Percentagem[i]
  ]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.