Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
How to insert null on the same line index of a different object?

I have 2 Objects with arrays

Edit*
imageSRC = (
https://store.com/image.png-2022,
https://store.com/image.png-2023,
https://store.com/image.png-2024,
https://store.com/image.png-2025,
https://store.com/image.png-2026,
https://store.com/image.png-2027,
.... (19 total)

ImageID = (
12345689,
45678915,
null,
34567890,
89123456,
89012345,
.... 22(total))
End Edit*

The imageSRC holds urls, and have a count of 19 items that are comma seperated The imageID holds image IDs, and have a count of 22 items that are comma seperated

The number of items for both arrays will vary based on different products.

These arrays represents variant images from my store of a product. Some of the imageIDs have null ("") values due to some variants not having images aka imageSRC.

The problem I am facing is that the imageSRC doesnt align with the imageID once the imageID hits a null value. The imageSRC and imageIDs are already in order by the variations index and cannot change.

So, the solution for me would be to insert a null value in the imageSRC object at the same exact index within the imageID object.

example

What is currently happening

https://store.com/image.png-2022 123456789
https://store.com/image.png-2023 145679879
https://store.com/image.png-2024 null
https://store.com/image.png-2025 891234567

What I want to happen

https://store.com/image.png-2022 123456789
https://store.com/image.png-2023 145679879
null null
https://store.com/image.png-2024 891234567

I do not want to get rid of the null values (using continue), as I am wanting to keep them as a "placeholder" for a different step in my setup. I should end up with the same length of indexes as the imageID when the code is executed. In this case 22.

I have tried everything from (push, continue, break, and etc.) and although I have came close several times. I'm still not able to get this right.

Here's the Javascript that I have been using ...

let imageSRC = inputData.imageSRC.split(",");
let imageID = inputData.imageID.split(",");

function toObject(imageSRC, imageID) {
 let result = {};
 for (let i = 0; i < imageID.length; i++) {
    if (imageID[i] === "") { continue;
      } result[imageSRC[i]] = imageID[i];
 }
   return result
}

output = toObject(imageSRC, imageID);`
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do the following :

let imageSRC = inputData.imageSRC.split(",");
let imageID = inputData.imageID.split(",");

function toObject(imageSRC, imageID) {
 const result = [];
 let j=0;
 // insert null at correct position
 for(let i=0; i<imageId.length; i++) {
   if(!imageId[i] || imageId[i] === 'null') {
     result.push(null)
   } else {
     result.push(imageSrc[j++]);
   }
 }
 // copy the remaining of imageSrc if any
 for(let i = j; i<imageSrc.length; i++) {
  result.push(imageSrc[i]);
 }
 return result;
}

output = toObject(imageSRC, imageID);`
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda