
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
object-squish
Advanced tools
A node.js utility for flattening objects to a single level.
Install object-squish via npm:
npm install object-squish --save
Then require in your file
var squish = require('object-squish');
Use object-squish to collapse pesky nested objects down to one level.
var data = {
spiders: { are: 'ok' },
they: {
just: {
want: 'to',
be: 'friends'
}
}
};
var flattened = squish(data);
// === Outputs ->
{
'spiders.are': 'ok',
'they.just.want': 'to',
'they.just.be': 'friends'
}
arrays are skipped over by default but can be included by passing the includeArrays option.
var data = { spiders: [1, 2, 3] };
var flattened = squish(data, { includeArrays: true });
// === Outputs ->
{
'spiders.0': 1,
'spiders.1': 2,
'spiders.2': 3,
}
See below for a comprehensive list of Options
Type: Number
Default value: Infinity
Set the depth at which to stop flattening.
Type: Boolean
Default value: false
Set whether to flatten arrays, keys will appear as array indexes 0, 1, 2, ....
Type: String
Default value: '.'
Set the seperating character/sequence in the derived path
Change the keys of the object, this can be one of:
'lowercase' -> ensure all keys are lowercase
'uppercase' -> ensure all keys are uppercase
Function -> a function which is passed the key and returns the modified key
A predicate, called with the object being processed, returning a boolean indicating whether to descend into the object or not.
var data = {
spiders: { are: 'ok' },
they: {
just: {
want: 'to',
be: 'friends'
}
}
};
// we can use modifyKey to make all the keys uppercase
var uppercased = squish(data, { modifyKey: 'uppercase' });
// === Outputs ->
{
'SPIDERS.ARE': 'ok',
'THEY.JUST.WANT': 'to',
'THEY.JUST.BE': 'friends'
}
// or use a function to perform more complex processing
var mutate = function (key) {
return key.replace('are', 'arent');
};
var modified = squish(data, { modifyKey: mutate });
// === Outputs ->
{
'spiders.arent': 'ok',
'they.just.want': 'to',
'they.just.be': 'friends'
}
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
FAQs
Flatten nested objects to a single level
We found that object-squish 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.