Coder Social home page Coder Social logo

Comments (2)

metroluffy avatar metroluffy commented on June 17, 2024 1
// 辅助函数
const isObj = obj => Object.prototype.toString.call(obj) === '[object Object]'

// 扁平化树形结构
function flattenNTreeHelper (data, ans, prevKey = '') {
  Object.keys(data).forEach(key => {
    const currKey = prevKey ? prevKey + '.' + key : key
    const currVal = data[key]
    if (isObj(currVal)) {
      flattenNTreeHelper(currVal, ans, currKey)
    } else {
      ans[currKey] = currVal
    }
  })
}
function flattenNTree (data) {
  if (!isObj(data)) return data
  const ans = {}
  flattenNTreeHelper(data, ans, '')
  return ans
}
// 扁平化结构转树形
function nTreeConstruct (data) {
  if (!isObj(data)) return data
  const ans = {}
  Object.keys(data).forEach(key => {
    // 按 . 分割key, 顺带支持一下形如 'z[0]'的格式
    const keys = key.replace(/\[(\d+)\]/g, '.$1').split('.')
    let curr = ans
    for (let i = 0; i < keys.length - 1; i++) {
      curr[keys[i]] = curr[keys[i]] || {}
      curr = curr[keys[i]]
    }
    curr[keys[keys.length - 1]] = data[key]
  })
  return ans
}

from fe-learning.

metroluffy avatar metroluffy commented on June 17, 2024

再考虑以下数据:

const data = {
  a: {
    b: {
      c: {
        dd: 'abcdd'
      }
    },
    e: 'ae',
    g: [1, { g1: 6 }, [1, 2]]
  }
}
// 期望输出
const output = {
   'a.b.c.dd': 'abcdd',
   'a.e': 'ae',
   'g[0]': 1,
   'g[1].g1': 6,
   'g[2][0]': 1,
   'g[2][1]': 2
}

也就是生成key的时候要多考虑一个数组类型,实现如下:

function flattenNTreeHelper (data, ans, prevKey = '', isArr = false) {
  Object.keys(data).forEach(key => {
    const currKey = prevKey ? (prevKey + (isArr ? ('[' + key + ']') : ('.' + key))) : key
    const currVal = data[key]
    const dataIsArr = Array.isArray(currVal)
    if (isObj(currVal) || dataIsArr) {
      flattenNTreeHelper(currVal, ans, currKey, dataIsArr)
    } else {
      ans[currKey] = currVal
    }
  })
}

from fe-learning.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.