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

98
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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