JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10901
  • Score
    100M100P100Q121109F
  • License MIT

Creates an object from an array of entries, in imitation of the Map constructor.

Package Exports

  • new-object

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (new-object) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

new-object

Creates an object from an array of entries.

The Map constructor lets you specify initial keys and values via an iterable collection (e.g. an array) of key-value pairs, whereas the Object constructor does not. This simple module fills that gap, approximating the ECMA-defined behavior for the Map constructor but for Objects.

In short, you can think of the new-object module as doing the opposite of Object.entries():

newObject(Object.entries({a: 1, b: 2})) // {a: 1, b: 2}

Remember that, unlike Maps, Objects can only use strings and symbols as keys.

Installation

Requires Node.js 6.0.0 or above.

npm i new-object

API

The module exports a single function.

Parameters

  1. entries (iterable): The key-value pairs for the object.
  2. Optional: Object argument:
    • throwIfEquivKeys (Error, string, or boolean): Set this to throw an error if entries contains keys that would be considered duplicates in the context of an object. For example, a Map can have keys that are objects, but those keys will all likely evaluate to [object Object] if made keys in an object. Similarly, a Map can have distinct 1 (number) and '1' (string) keys, but these would be considered the same in an object context.

Return Value

Returns a plain object containing entries.

Example

const newObject = require('new-object')

const entries = [['a', 1], ['b', 2]]

// What was possible for a Map...
const map = new Map(entries)
map.get('a') // 1

// ...is now also possible for an Object:
const obj = newObject(entries)
obj.a // 1
  • construct-map: Like this module, except it can also construct Maps and other key/value collections.