@blumintinc/flatten-anything

中文文档
4.1.1发布于 2025/06/11MITTypeScriptESM + CJS近 30 天下载 5112

Flatten objects and replace nested props with 'prop.subprop'. A simple and small integration

原始使用文档
文档目录9 节
  1. CommonJS Support
  2. Meet the family (more tiny utils with TS support)
  3. Usage
  4. Flatten objects
  5. Flatten arrays
  6. Limit the depth to flatten
  7. Flatten only certain props
  8. Treeshaking
  9. Example for Firestore

Flatten anything 🏏

Total Downloads Latest Stable Version

npm i flatten-anything

Flatten objects and replace nested props with 'prop.subprop'. A simple and small integration.

CommonJS Support

This package provides both ESM and CommonJS builds:

// ESM (default)
import { flatten } from 'flatten-anything'

// CommonJS
const { flatten } = require('flatten-anything')

I was looking for:

  • A simple solution to flatten objects & arrays
  • Only flatten plain objects and not special class instances!

This last one is crucial! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to flatten them. So we gotta be careful!

flatten-anything will flatten objects and their nested properties, but only as long as they're "plain objects". As soon as a sub-prop is not a "plain object" and has a special prototype, it will stop flattening there and reference that instance "as is". ♻️

Very usable for creating a payload for Firebase Firestore update function, which only accepts flat objects! With Firestore for example Firebase.firestore.FieldValue.delete() does not break.

Can be used in combination with nestify-anything, which does the exact opposite of this one! 😉

Meet the family (more tiny utils with TS support)

Usage

  • If you pass an object, it will flatten all nested properties
  • If you pass an array, it will flatten all nested arrays

Flatten objects

import { flatten } from 'flatten-anything'

const target = { name: 'Ho-oh', types: { fire: true, flying: true } }

flatten(target)
// returns {
//   name: 'Ho-oh',
//   types.fire: true,
//   types.flying: true
// }

Please note that when you pass an object it will only flatten nested object props, and do nothing to arrays inside the object.

Flatten arrays

import { flatten } from 'flatten-anything'

const target = [1, 2, ['a', 'b', ['y', 'z']], 3]

flatten(target)
// returns [1, 2, 'a', 'b', 'y', 'z', 3]

Please note that when you pass an array it will only flatten direct arrays, and do nothing to objects inside the array.

Limit the depth to flatten

currently only works with objects. Please open an issue if you want it to work with arrays as well!

const target = {
  name: 'Lugia',
  appearence: {
    // let's only flatten 1 level until here:
    parts: { wings: true },
    colors: { white: true, blue: true },
  },
}
const untilDepth = 1

flatten(target, untilDepth)
// returns {
//   name: 'Lugia',
//   appearence.parts: { wings: true },
//   appearence.colors: { white: true, blue: true },
// }

Flatten only certain props

It's possible to only flatten eg. one prop.

import { flattenObjectProps } from 'flatten-anything'

const target = {
  appearence: { hair: 'orange' },
  traits: { strength: 9000 },
}
const propsToFlatten = ['traits']

flattenObjectProps(target, propsToFlatten)
// returns {
//   appearence: { hair: 'orange' },
//   traits.strength: 9000,
// }

You can also point to a nested object property to only flatten specific nested props, but keep the rest as an object!

const target = {
  traits: {
    strength: 9000,
    range: { min: 8000, max: 10000 },
  },
}

const propsToFlatten = ['traits.range']
// only flatten `traits.range` and nothing else.

flattenObjectProps(target, propsToFlatten)
// returns {
//   traits: { strength: 9000 },
//   'traits.range': {min: 8000, max: 10000},
// }

Treeshaking

Importing flatten allows you to use it for both objects and arrays.

If you use webpack, rollup, etc. you can import less code by specifying the exact flatten function you need:

import { flattenObject } from 'flatten-anything'
// or
import { flattenArray } from 'flatten-anything'

Example for Firestore

const pokemon = {
  name: 'Charizard',
  types: { dark: true, fire: true, flying: true },
}
// we want to delete the `dark` type from this Pokemon

const payload = flatten({
  types: { dark: Firebase.firestore.FieldValue.delete() },
})
Firebase.firestore()
  .doc('pokemon/charizard')
  .update(payload)

版本与发布记录

@blumintinc/flatten-anything 共发布 8 个版本,首次发布于 2025/04/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.1.1latest2025/06/111.4 年前
4.1.02025/05/241.5 年前
4.0.82025/04/061.6 年前
4.0.72025/04/061.6 年前
4.0.62025/04/061.6 年前
4.0.52025/04/061.6 年前

关于 @blumintinc/flatten-anything 的常见问题

@blumintinc/flatten-anything 是什么?

Flatten objects and replace nested props with 'prop.subprop'. A simple and small integration

如何安装 @blumintinc/flatten-anything?

在项目目录运行 npm install @blumintinc/flatten-anything,或使用 pnpm add / yarn add / bun add。

@blumintinc/flatten-anything 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@blumintinc/flatten-anything 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@blumintinc/flatten-anything 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@blumintinc/flatten-anything 可以商用吗?

@blumintinc/flatten-anything 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@blumintinc/flatten-anything 的下载量有多少?

根据 npm 官方下载统计,@blumintinc/flatten-anything 在 2026/10/09 – 2026/11/07 这 30 天内被下载 5,112 次,日均约 170 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@blumintinc/flatten-anything 还在维护吗?

最新版本 4.1.1 发布于 2025/06/11(1.4 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/04/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。