Menu

gitpiper

indexOn javascript Code Snippet in 2024

arrayobjectintermediate

Last Updated: 4 December 2024

Creates an object from an array, using the specified key and excluding it from each value.

  • Use Array.prototype.reduce() to create an object from arr.
  • Use object destructuring to get the value of the given key and the associated data and add the key-value pair to the object.
const indexOn = (arr, key) => arr.reduce((obj, v) => { const { [key]: id, ...data } = v; obj[id] = data; return obj; }, {});
indexOn([ { id: 10, name: 'apple' }, { id: 20, name: 'orange' } ], 'id'); // { '10': { name: 'apple' }, '20': { name: 'orange' } }

javascript snippet similar to indexOn For You in December 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️