Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!