Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
How to add more objects to an array of objects continuing the numbering but not start over?

I have this minor issue but I'm so close.

I have a Array of Objects with 100 OBJECTS in it. [0-99]

I want to add another 100 or more objects to the end of the current object 99.

What I'm getting is [0-99], 100: [0-99]

Let me show you the code:

addEntry = (newEntry) => {

    let newSortedData = [];

    // Parse any JSON previously stored in allEntries
    let existingEntries = JSON.parse(sessionStorage.getItem("magic1")).data;
    if (existingEntries === null) {
        existingEntries;
    }
    sessionStorage.setItem("magic1", JSON.stringify(newEntry));
    // Save allEntries back to local storage
    existingEntries.push(newEntry[0].data);

    console.log("existing entries with push for new ones: ", existingEntries);

    table.clear().draw();
    newSortedData.push(existingEntries);
    table.rows.add(_.sortBy(newSortedData, "title")); // Add new data
    table.columns.adjust().draw(); // Redraw the DataTable

    console.log("Existing and new ones sorted: ", newSortedData[0].data);

    sessionStorage.setItem("magic1", JSON.stringify(newSortedData[0].data));

};

What I'm getting is this:

magic1 starts out with 100 OBJECTS in the array. Where I'm getting the data from, there are 7000 items/products. I'm using a PHP to pull the data from the source. They only come in pages 1 - 70 with 100 objects in each page. hence, 7000 objects. It's a BIZARRE way of me having to do this but I have to PING the server going past 100, 201, 301, 401, 501, 601, and so on, through all 70 hits to the server, 100 items at a time. They just can't give me a getRowcount() as rowcount from the SQL. I have to continually ping or hit the server until I get a number of items LESS than 100 meaning, I've hit the last page of less than 100.

Here's what the end of one array looks like and the beginning of the new one.

All I want to do is to KEEP appending the additional pages to continue the count with 100, 101, 102, all the way to 199. Then 200, 201, 201 - 299 and so on. Apparently obj.push(newObj) does what you see in the pic.

enter image description here

OF NOTE: when I get to this part of the code:

existingEntries.push(newEntry[0].data);

both the existingEntries and newEntry[0].data have ONLY objects. There is NO prefix like data: ... for example. So, why can't I just append then SORT everything by "title" alphabetically with 0 - 199 instead of 0-99, 100: 0-99???

Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If newEntry[0].data is an array you can spread it out:

existingEntries.push(...newEntry[0].data);

or just use concat instead of push:

existingEntries.concat(newEntry[0].data)
about 4 years ago · Juan Pablo Isaza Relatório

0

I assume the issue is in concatenating the two arrays (existingEntries and newEntry[0].data). Array.push() does not combine two arrays but puts the other array at the end of the array.

let a1 = ['a', 'b'], a2 = ['c', 'd'];
a1.push(a2);
console.log(a1) // ['a', 'b',  ['c', 'd']]

You can use Array.concat() to do it.

let a1 = ['a', 'b'], a2 = ['c', 'd'];
a1 = a1.concat(a2);
console.log(a1) // ['a', 'b', 'c', 'd']

Note Array.concat() returns a new array.

Changes:

// this
existingEntries.push(newEntry[0].data);
// to this
existingEntries = existingEntries.concat(newEntry[0].data);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda