将JS对象隐藏到具有父名称的平面数组中
Covert JS object into flat array with parent names
我有一个这样的对象:
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;
}
基于闭包的答案
相关文章:
- array_push平面数组问题-需要能够向数组中添加多个变量
- 用Javascript从两个平面结构数组中格式化JSON
- 如何从平面JavaScript数组创建分层HTML结构
- 强制json_encode在平面数组中创建数值字符串
- Undercore:对象到平面对象的数组.什么'这就是魔法
- 将JS对象隐藏到具有父名称的平面数组中
- 在javascript中从平面数组构建树数组
- 将嵌套的整数数组转换为平面整数数组-javascript
- 如何将平面JavaScript对象数组转换为嵌套的分层JSON结构
- JavaScript从给定宽度和高度的平面数组中确定行、列和元素的相对位置
- 如何从平面数组创建JavaScript多维数组
- 将平面的对象数组转换为嵌套的对象数组
- 将对象重构为平面数组
- JS - Lodash;嵌套对象(父/子)到平面数组
- 从平面数组创建树对象
- Js平面数组到多级数组
- 在JavaScript中从平面数组生成树结构(不使用对象引用)
- 如何将嵌套的 JavaScript 对象转换为平面数组
- 使用平面数组过滤JSON对象
- 将平面数组结构为树