From 3a03035a5b08e4a7ab07c7fa2af26a5e0b54026a Mon Sep 17 00:00:00 2001 From: gholk Date: Fri, 30 Jun 2023 21:51:59 +0800 Subject: [PATCH] Distinct array or object by keep a empty array literal Make `{a: [1,2]}` produce `{a: [], 'a.0': 1, 'a.1': 2}`, so we can distinct array and object. Add a `opts.useEmptyArray` option to turn on this feature. --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index 42e1ddc..8f923d0 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,9 @@ export function flatten (target, opts) { if (!isarray && !isbuffer && isobject && Object.keys(value).length && (!opts.maxDepth || currentDepth < maxDepth)) { + if (opts.useEmptyArray && Array.isArray(value)) { + output[newKey] = [] + } return step(value, newKey, currentDepth + 1) }