deepSet

is compatible to call with Array

Definition

function deepSet(keys: string[], value: any) : this;
function deepSetP(keys: string[], value: any) : this;

Example

As the name implies, goes ballsdeep into an object to assign the provided value to the associated key or nested keys

import '@marthvon/protopp';
// or
import '@marthvon/protopp/objectpp/methods/deepSet.js';

const o1 = { a: {} };
const o2 = {};

o1.deepSet([ 'a', 'b' ], 1);
console.log(o1);
// Outputs { a: { b: 1 } }

o2.deepSet([ 'a', 'b' ], 1);
// Error!!! key “a” doesn't exists

// Unless...
o2.deepSetP([ 'a', 'b' ], 1);
console.log(o2);
// Outputs { a: { b: 1 } }

So what's the difference?... deepSet will throw and error whenever any of the nested keys doesn't already exist in the object, except for the last key. While, deepSetP will create the missing nested keys. NOTE: it may overwrite existing associated value of key if value isn't an object or an array.

Why you may ask?...

Well... I mean... Who actually enjoys doing this?...

if( a['b'] && a['b']['c'] )
a.b.c.d = 1;

Ask yourself this question, what if I kept increasing the object depth?

NPM Homepage
Github Homepage
About Me