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

96
Views
Enumerating sliced array using original indices

I need to slice an array and enumerate the values but I also need the reference to the original index because I'm doing an async operation that needs to be mapped back to the original index in the original array when complete.

const array = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge'];
const slicedArray = array.slice(0, 3).map((v, i) => ({ v, i }));
// Returns: [{ "v": "foo", "i": 0 }, { "v": "bar", "i": 1 }, { "v": "baz", "i": 2 }]
// Required: [{ "v": "foo", "i": 0 }, { "v": "bar", "i": 1 }, { "v": "baz", "i": 2 }]
const slicedArray2 = array.slice(3, 6).map((v, i) => ({ v, i }));
// Returns: [{ "v": "qux", "i": 0 }, { "v": "quux", "i": 1 }, { "v": "corge", "i": 2 }]
// Required: [{ "v": "qux", "i": 3 }, { "v": "quux", "i": 4 }, { "v": "corge", "i": 5 }]

How can I achieve this?

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

0

The items in the slice always have indexes that are exactly startIndex less than in the original array:

slice(0, 3) -> [0, 1, 2] -> [0 - 0, 1 - 0, 2 - 0]
slice(3, 6) -> [0, 1, 2] -> [3 - 3, 4 - 3, 5 - 3]
slice(n, …) -> [0, 1, …] -> [n + 0 - n, n + 1 - n, n + 2 - n, …]

So, just adding startIndex back should do the trick:

/**
 * @template Item
 * @param {Item[]} items
 * @param {number} startIndex
 * @param {number} [endIndex]
 * @returns {Array<{ item: Item; index: number }>}
 */
function slice(items, startIndex, endIndex = items.length) {
    return items.slice(startIndex, endIndex).map((item, index) => ({
        item,
        index: index + startIndex,
    }));
}

function slice(items, startIndex, endIndex = items.length) {
  return items.slice(startIndex, endIndex).map((item, index) => ({
    item,
      index: index + startIndex,
  }));
}

const letters = [ ...'abcdefghijklmnopqrstuvwxyz' ];

console.log(slice(letters, 0, 3));
console.log(slice(letters, 3, 6));
console.log(slice(letters, 6, 9));

about 4 years ago · Juan Pablo Isaza Report

0

Using array.map() You can get the all array value and index ! After this use .slice()

Try this code it's help you

 const array = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge'];
 const slicedArray2 = array.map((v, i) => ({ v, i })).slice(3, 6);
 console.log(slicedArray2, 'slicedArray2');
about 4 years ago · Juan Pablo Isaza Report

0

I actually like the accepted answer better, but for the record another solution without the slice

const data = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge'];

const slice = (items, startIndex, endIndex = items.length) => {
  return items.reduce((acc, v, i) => {
    if (i >= startIndex && i < endIndex )
      acc.push({ v, i })
    return acc;
  }, []);
}

console.log(slice(data, 0, 3));
console.log(slice(data, 3));
.as-console-wrapper{min-height: 100%!important; top: 0}

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!