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

114
Views
Use Array.filter to return values for a set number of items

I currently have a function that returns a value for each item in my array, up to a maximum number. It looks like this

const myArray = [
    { url: "example.com/1", other: "foo" },
    { url: "example.com/sdf", other: "foo"  },
    { url: "example.com/blue", other: "foo"  },
    { url: "example.com/foo", other: "foo"  },
    { url: "example.com/123", other: "foo"  },
];

function getNumberOfUrls(data, num) {
  const newArray = [];

  data?.forEach(function (datum) {
    if (newArray.length < num) {
      newArray.push(datum.url);
    }
  });

  return newArray;
}

// Output
//["example.com/1", "example.com/sdf", "example.com/blue"] 

It simply returns the url for each object in the array until it hits the limit provided. It works fine, but I was trying to explore if there is a more suitable Array function I should be using.

I know Array.filter creates a new array based on whether the iterated item passes a particular condition, but I wondered if it could be used to check whether something else passes the condition - in this case the parent array.

function getNumberOfUrls(data, num) {
  return data.filter(datum => /* return url until we hit .length === num in data? */ )
};

How can I make this work or is there a better-suited Array method to achieve this?

ETA: the original example array didn't paint the full picture. I've now added more data to show the issue. I don't want to return an array with just the first three objects, I want to just return the url value from the first three objects.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The shortest way to do this is using Array.slice:

const myArray = [
  { url: "example.com/1" },
  { url: "example.com/sdf" },
  { url: "example.com/blue" },
  { url: "example.com/foo" },
  { url: "example.com/123" },
]

const limit = 3
const shorterArray = myArray.slice(0, limit).map(item => item.url)

console.log(shorterArray)

I removed my other code, as it was inefficient, and should not be used.

about 4 years ago · Juan Pablo Isaza Report

0

One approach would be use Array.from() and it's internal mapper

const myArray = [
    { url: "example.com/1" },
    { url: "example.com/sdf" },
    { url: "example.com/blue" },
    { url: "example.com/foo" },
    { url: "example.com/123" },
];

function getNumberOfUrls(data, num) {
  return Array.from({length:num}, (v,i) => data[i].url)
}

console.log(getNumberOfUrls(myArray, 3))

about 4 years ago · Juan Pablo Isaza Report

0

You could simply set the length of the newly mapped array

Edit: The .map() is used as the final array is different to the starting array (just urls in final as opposed to objects in starting array)

const myArray = [
    { url: "example.com/1" },
    { url: "example.com/sdf" },
    { url: "example.com/blue" },
    { url: "example.com/foo" },
    { url: "example.com/123" },
];

function getNumberOfUrls(data, num) {
  
  let newArray = data.map(el => el.url);
  newArray.length = num;

  return newArray;
}

console.log(getNumberOfUrls(myArray, 3))

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!