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

Muowee
  • 54
  • 8
Alexis Garcia
  • 452
  • 3
  • 15
  • You are overwriting that `$i` variable with every loop. – VLAZ Jul 04 '19 at 13:08
  • `DataSend[$i] = ['CodProduto',...]` – adiga Jul 04 '19 at 13:08
  • https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – chandu20 Jul 04 '19 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. – Mulan Jul 04 '19 at 13:39

3 Answers3

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?

Michael Beeson
  • 2,840
  • 2
  • 17
  • 25
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]);
obscure
  • 11,916
  • 2
  • 17
  • 36
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]
  ]);
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79