将JS对象隐藏到具有父名称的平面数组中

Covert JS object into flat array with parent names

本文关键字:平面 数组 对象 JS 隐藏      更新时间:2023-09-26

我有一个这样的对象:

data:
{
    connection:
    {
        type: 0,
        connected: false
    },
    acceleration:
    {
        x: 0,
        y: 0,
        z: 0,
        watchId: 0,
        hasError: false
    }        
},

将其转换为像这样的平面阵列:

"connected": false
"hasError": false
"type": 0
"watchId": 0
"x": 0
"y": 0
"z": 0

这是一项容易的任务(复发是你的朋友!)。

但是,在Javascript中,有没有任何方法可以通过所谓的完全父级来获得它,即类似这样的东西:

"connection.connected": false
"acceleration.hasError": false
"connection.type": 0
"acceleration.watchId": 0
"acceleration.x": 0
"acceleration.y": 0
"acceleration.z": 0

还是我期待太多?

另一种变体:

function flatten(o) {
  var prefix = arguments[1] || "", out = arguments[2] || {}, name;
  for (name in o) {
    if (o.hasOwnProperty(name)) {
      typeof o[name] === "object" ? flatten(o[name], prefix + name + '.', out) : 
                                    out[prefix + name] = o[name];
    }
  }
  return out;
}

flatten(data); 一样调用

总有一种方法,但请注意,这两种方法都是对象,也不是数组。(Javascript中的关联数组只是对象)。

function objectFlatten( o , n ) {
        var p   =   {}
            ,   n = n? n : ''
            ,   merge = function(a,b){ for( k in b) a[k] = b[k]; return a;}
            ;
        for( i in o ) {
            if( o.hasOwnProperty( i ) ) {
                if( Object.prototype.toString.call( o[i] ) == '[object Object]' || Object.prototype.toString.call( o[i] ) == '[object Array]')
                    p = merge( p , objectFlatten( o[i] , n? n + '.' + i : i ) );
                else
                    p[i] = o[i];
                }
            }
        return p;
   }

为了子孙后代,请查看我为Forms JS编写的Flatten或类似的实用程序。

我的五美分。

对于想要展平对象但不想要属性的情况

换句话说。

你有这样的东西:

var obj = {
  one_a: {
    second_a: {
      aaa: 'aaa',
      bbb: 'bbb'
    },
    second_b: {
      qqq: 'qqq',
      third_a: {
        www: 'www',
        eee: 'eee',
        fourth_a: {
          'rrr': 'rrr',
          fifth: {
            ttt: 'ttt'
          }
        },
        fourth_b: {
          yyy: 'yyy',
        }
      },
      third_b: {
        'uuu': 'uuu'
      }
    }
  },
  one_b: {
    iii: 'iii'
  }
}

并且希望使嵌套对象平面化,但不希望平面化属性:

{ 'one_a second_a ': { aaa: 'aaa', bbb: 'bbb' },
  'one_a second_b ': { qqq: 'qqq' },
  'one_a second_b third_a ': { www: 'www', eee: 'eee' },
  'one_a second_b third_a fourth_a ': { rrr: 'rrr' },
  'one_a second_b third_a fourth_a fifth ': { ttt: 'ttt' },
  'one_a second_b third_a fourth_b ': { yyy: 'yyy' },
  'one_a second_b third_b ': { uuu: 'uuu' },
  'one_b ': { iii: 'iii' } }

代码:

function flatten (obj, includePrototype, into, prefix) {
  into = into || {};
  prefix = prefix || "";
  for (var k in obj) {
    if (includePrototype || obj.hasOwnProperty(k)) {
      var prop = obj[k];
      if (prop && typeof prop === "object" && !(prop instanceof Date || prop instanceof RegExp)) {
        flatten(prop, includePrototype, into, prefix + k + " ");
      }
      else {
        if (into[prefix] && typeof into[prefix] === 'object') {
          into[prefix][k] = prop
        } else {
          into[prefix] = {}
          into[prefix][k] = prop
        }
      }
    }
  }
  return into;
}

基于闭包的答案