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

135
Views
How to limit an array field in javascript object to a certain length

I know this may a simple problem but I have a the following javascript object:

const categories = {
    title: 'cat1',
    contents: [
        {
            name: 'cont1'
        },
        {
            name: 'cont2'
        },
        {
            name: 'cont3'
        }
    ]
}

How can a transform this categories object so it has only 2 contents element as an example?

const transformedCategories = {
    title: 'cat1',
    contents: [
        {
            name: 'cont1'
        },
        {
            name: 'cont2'
        }
    ]
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A couple of ways to do it:

const transformedCategories = {
  title: categories.title,
  contents: categories.contents.slice(0,2) // you can also use splice here
}

Another, slightly cheeky way:

const contents = [...categories.contents];
contents.length = 2;
const transformedCategories = {...categories, contents}
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!