![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
object-property-extractor
Advanced tools
A lightweight (no dependencies) tool to extract deeply nested values from JS Objects (or Arrays), with optional Fallback.
Access deep object properties using a string (e.g. "user.country.name")
A lightweight (no dependencies) tool to extract deeply nested values from JS Objects (or Arrays), with optional Fallback.
Similar to Lodash' get
function, but with some additional functionality.
Consider the object
const data = {
user: {
name: { first: 'Jango', last: 'Fett' },
children: ['Boba', 'Clone 1', 'Clone 2', ...etc],
weapons: [
{ name: 'Blaster', description: 'For shooting stuff' },
{ name: 'Seismic charge', description: '...BWAAAAAANG' },
],
},
...otherProperties,
}
In Javascript, you call inner object properties via dot notation:
data.user.name.last // Fett
If you want to access a property dynamically, you can do this:
const key = "user"
return data[key]
However, you can't do this:
const key = "user.name"
return data[key]
This tool allows access to deep properties from a single "property path" string.
yarn add object-property-extractor
// OR
npm install object-property-extractor
extract( dataObject, propertyString, [fallback] )
import extract from "object-property-extractor"
// Using the data object above
extract(data, "user.name.first") // Jango
// With fallback when path not found
extract(data, "user.age", "Unknown") // Unknown
// Arrays can be accessed by index, as per normal indexing syntax
extract(data, "user.children[1]") // Boba
In addition to accessing array by index (above), if an array consists of objects, then it's possible to exract a single property from each object in the returned array.
For example:
extract(data, "user.weapons.name")
// ["Blaster", "Seismic charge"]
Note that this is essentially a shorthand for:
extract(data, "user.weapons").map((weapon) => weapon.name)
If a requested property can't be accessed (e.g. incorrect path), the function will throw an error, unless a fallback is provided. So unless you are catching an handling these errors at a higher level, it is recommended to always provided a fallback (null
is an acceptable fallback).
A jest test suite is included in the repo. To run:
yarn test
See /test/test.ts
for the test cases.
Please make an issue in the Github repo: https://github.com/CarlosNZ/object-property-extractor
FAQs
A lightweight (no dependencies) tool to extract deeply nested values from JS Objects (or Arrays), with optional Fallback.
The npm package object-property-extractor receives a total of 27,217 weekly downloads. As such, object-property-extractor popularity was classified as popular.
We found that object-property-extractor demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.