3

我有一个这样的对象:

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 中是否有任何方法可以通过所谓的full parents获得它,即像这样:

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

还是我期待太多?

4

4 回答 4

3

总有办法,但请注意,这两个都是对象,数组都不是。(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;
   }
于 2013-08-07T16:44:48.320 回答
3

另一种变体:

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);

于 2013-08-07T17:16:46.523 回答
2

对于后代-检查flat或我为 Forms JS 编写的类似实用程序Flatten

于 2015-03-15T00:10:11.297 回答
1

我的五分钱。

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

换句话说。

你有这样的事情:

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;
}

基于闭包的回答

于 2016-10-14T08:20:31.323 回答