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

96
Vistas
JS Array.push acts in unexpected way

I am experiencing unexpected behaviour of push function. The problem is with the latest line of code cited below.

export enum non_searchFieldsNames {
    language = 'language',
    categories = 'categories',
    subtitle = 'subtitle',
    publishedDate = 'publishedDate',
    id = 'id',
}
enum columnsEnum {
    title,
    authors,
    language,
    categories,
    subtitle,
    publishedDate,
}

function toArray(obj: header | contentCategories | sourceFields) {
    return Object.entries(obj)
        .sort((a, b) => {
            return +a - +b;
        })
        .map(item => item[1]);
}

let sourceFieldsObject: sourceFields = {
    [columnsEnum.title]: searchFieldsNames.title,
    [columnsEnum.authors]: searchFieldsNames.authors,
    [columnsEnum.language]: non_searchFieldsNames.language,
    [columnsEnum.categories]: non_searchFieldsNames.categories,
    [columnsEnum.subtitle]: non_searchFieldsNames.subtitle,
    [columnsEnum.publishedDate]: non_searchFieldsNames.publishedDate,
};

const sourceFieldsArray = toArray(sourceFieldsObject).push(non_searchFieldsNames.id);


The problem is with the latest line of code. The value I do receive here is 7. When I simplify it like this

const sourceFieldsArray = toArray(sourceFieldsObject)

I receive an array(however, without the value I try to add, of course).

When I split the logic

const sourceFieldsArray = (toArray(sourceFieldsObject));
sourceFieldsArray.push(non_searchFieldsNames.id);

I get what I wanted. Anyway, I would like to have it as one-liner. So, what is my error? I have tried also

const sourceFieldsArray (toArray(sourceFieldsObject)).push(non_searchFieldsNames.id)

But it does not help.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

push modifies the main array directly, and it does not return a new array as you expected, but the count of items in that array.

You can check the below demo for push's returned value

const array = [1,1,1] //3 items

const count = array.push(1) //1 more item

console.log(count) //total is 4 items

If you want to have a one-liner, you can try the cloning approach with the spreading operator

const sourceFieldsArray = [...toArray(sourceFieldsObject), non_searchFieldsNames.id]

Side note that this approach means you're creating a new array completely, so you need to be aware of some performance situations with a huge array.

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