
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
nested-objects-util
Advanced tools
A module to filter and diff complex nested objects having circular references.
A minimalist inspection module to filter and diff complex nested objects having circular references.
It was implemented to filter out some values from inconveniently nested objects with circular references and then diff them.
It's designed to work both on nodejs and browser without any dependencies or additional polyfills.
Performance is currently not the key feature of this module, so please use more optimised libraries for e.g. deep equal.
npm install --save nested-objects-util
yarn add nested-objects-util
Or just copy index.min.js
into the console and start using NestedObjectsUtil
object.
const nestedObjectsUtil = require('nested-objects-util')
Flatten object by keys composed from own nested properties.
nestedObjectsUtil.flatten({
keyA: {
keyE: ['value3', 'value4'],
keyF: null,
keyD: 'value2',
keyB: {
keyC: 'value'
}
}
})
returns:
{
"keyA.keyE.0": "value3",
"keyA.keyE.1": "value4",
"keyA.keyF": null,
"keyA.keyD": "value2",
"keyA.keyB.keyC": "value"
}
with sortKeys=true it would return:
{
"keyA.keyB.keyC": "value",
"keyA.keyD": "value2",
"keyA.keyE.0": "value3",
"keyA.keyE.1": "value4",
"keyA.keyF": null
}
Unflatten object by keys composed from own nested properties.
nestedObjectsUtil.unflatten({
'keyA.keyB.keyC': 'value',
'keyA.keyD': 'value2'
})
returns:
{
"keyA": {
"keyB": {
"keyC": "value"
},
"keyD": "value2"
}
}
Access object's nested property.
const nestedObject = {
keyA: {
keyB: {
keyC: 'value'
}
}
}
nestedObjectsUtil.accessProperty('keyA.keyB.keyC', nestedObject)
returns:
"value"
Discard circular references (to avoid "Converting circular structure to JSON" error).
const a = {
b: 1
}
a.c = a
nestedObjectsUtil.discardCircular(a)
returns:
{
"b": 1,
"c": "~"
}
Filter a nested object by value or values (if array passed). Strict comparison is performed.
const a = {
b: {
c: 'str',
d: 'str2'
},
e: 'str',
f: {
g: {
h: 'str',
i: 'str3'
},
j: 'str4'
},
k: ['str', 'str5']
}
a.l = a.b
nestedObjectsUtil.filterValue(a, 'str')
returns:
{
"b": {
"c": "str"
},
"e": "str",
"f": {
"g": {
"h": "str"
}
},
"k": ["str"]
}
or with flattenFlag = true
nestedObjectsUtil.filterValue(a, 'str', true)
returns:
{
"b.c": "str",
"e": "str",
"f.g.h": "str",
"k.0": "str"
}
On browser with HTML5 download API: stringify, format and download the object.
Else: return stringified text.
const a = {
b: 1,
c: {
d: 2,
e: 2
}
}
a.f = a
a.g = a.f
const obj = nestedObjectsUtil.discardCircular(a)
nestedObjectsUtil.downloadStringified(obj)
returns:
{
"b": 1,
"c": {
"d": 2,
"e": 2
},
"f": "~",
"g": "~"
}
Compare two objects against each other after discarding circular references, flattening and ordering keys.
const objectA = {
keyA: {
keyB: {
keyC: 'value'
},
keyD: 'value2',
keyE: ['value3', 'value4']
}
}
objectA.circular = objectA
const objectB = {
keyA: {
keyE: ['value3', 'value4'],
keyD: 'value2',
keyB: {
keyC: 'value'
}
}
}
objectB.circular = objectB
nestedObjectsUtil.areObjectsEqual(objectA, objectB)
returns:
true
It is also possible to skip certain properties when comparing the objects by providing an array to string properties.
const objectA = {
keyA: {
keyB: {
keyC: 'value'
},
keyD: 'value2',
keyE: ['value3', 'value4']
}
}
objectA.circular = objectA
const objectB = {
keyA: {
keyB: {
keyC: 'DIFFERENT_VALUE'
},
keyD: 'value2',
keyE: ['value3', 'value4']
}
}
objectB.circular = objectB
nestedObjectsUtil.areObjectsEqual(objectA, objectB, ['keyA.keyB.keyC'])
returns:
true
Get the properties which differ between object A and object B and return only those from object B.
const objectA = {
keyA: {
keyB: {
keyC: 'value'
},
keyD: 'value2',
keyE: ['value3']
}
}
objectA.circular = objectA
const objectB = {
keyA: {
keyB: {
keyC: 'value'
},
keyD: 'value2_CHANGED',
keyE: ['value3_CHANGED']
}
}
objectB.circular = objectB
nestedObjectsUtil.getObjectsDiff(objectA, objectB)
returns:
{
"keyA": {
"keyD": "value2_CHANGED",
"keyE": ["value3_CHANGED"]
}
}
In the browser, NestedObjectsUtil object is exposed either to window or with AMD.
Filter out 'abcd' value from the flattened object and download stringified JSON via HTML5 API with:
const object = NestedObjectsUtil.filterValue(App.SomeHugeObject, 'abcd', true)
NestedObjectsUtil.downloadStringified(object)
Discar circular references from the object, then flatten and download it.
let object = NestedObjectsUtil.discardCircular(App.SomeHugeObject)
object = NestedObjectsUtil.flatten(object)
NestedObjectsUtil.downloadStringified(object)
npm run test
yarn test
FAQs
A module to filter and diff complex nested objects having circular references.
We found that nested-objects-util demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.