appendInsert
Method is IN-PLACE. Also, compatible to call with Array
Definition
function appendInsert(key: string, ...values: any[]);
Example
Inspired by a php feature, this functionality either creates a new array containing the provided values to associate with the provided key, given there was no initial array associated to that key. Or, it appends the given values to the array of the associated key.
import '@marthvon/protopp';
// or
import '@marthvon/protopp/objectpp/methods/appendInsert.js';
const o = { a: undefined };
o.appendInsert('a', 1)
console.log(o);
// Outputs { a: [ 1 ] }
o.appendInsert('a', 2, 3)
console.log(o);
// Outputs { a: [ 1, 2, 3 ] }
Why you may ask?...
You'd be surprised of how useful it is when used in loops, especially when your handling unstructured/uncleaned data.
Primarily, this was inspired by the following code in PHP which appends or creates a new array in the location of key “a”
$myMap['a'][''] = 1;