展平嵌套的 json 并获取对象键
Flatten a nested json and get object keys
我从嵌套的客户端得到 json,我想破坏它并制作子对象键,最好使用 underscore.js。
例如我的 json:
var data = {
or:[
{
dealershipCompany : 11
},
{
authType: 'google'
}],
and: [
{
or: [
{
firstName: {'contains': 'search'}
},
{
lastName: {'contains': 'search'}
},
{
email: {'contains': 'search'}
}]
}]
};
我想同时删除 'or' 和 'and'
当我使用 Object.keys(data) 获取对象键时,我得到
['0','1','2','3','4','5']
但我希望它是这样的
['dealershipCompany', 'authType', 'firstName', 'lastName','email']
我尝试了几次自己压平它,但总是对象键被编号
这里是jsFiddlelink
这应该有效:
var data = {
or:[
{ dealershipCompany : 11 },
{ authType: 'google' }
],
and: [ {
or: [
{ firstName: {'contains': 'search'} },
{ lastName: {'contains': 'search'} },
{ email: {'contains': 'search'} }
]
}]
};
function getOnlyObjects(data) {
var result = [];
if (Array.isArray(data)) {
data.forEach(function(item) {
result = result.concat(
getOnlyObjects(item)
);
});
}
else {
Object.keys(data).forEach(function(key) {
if (Array.isArray(data[key])) {
result = result.concat(
getOnlyObjects(data[key])
);
}
else {
result = result.concat(data);
}
});
}
return result;
}
function getData(data) {
return getOnlyObjects(data).map(function(item) {
return Object.keys(item)[0];
});
}
console.log(getData(data));
输出:
["dealershipCompany", "authType", "firstName", "lastName", "email"]
当你在数组上使用 Object.keys
时,你会得到索引,因此你得到 ['0','1','2','3','4','5']
.
编辑 1
已将 === 'and'
、=== 'or'
迁移到阵列 exceptionList
。您可以添加更多需要过滤的键。这将使过滤易于管理并保持清洁。
代码
var data = {
or: [{
dealershipCompany: 11
}, {
authType: 'google'
}],
and: [{
or: [{
firstName: {
'contains': 'search'
}
}, {
lastName: {
'contains': 'search'
}
}, {
email: {
'contains': 'search'
}
}, ]
}]
};
var result = [];
// You can add further keys that you want to filter
var exceptionList = ["and", "or"];
function getKeys(obj) {
var _keys = Object.keys(obj);
_keys.forEach(function(key) {
// Check if key is either,`and`, `or`, or an index of array.
if (exceptionList.indexOf(key) >=0 || !isNaN(key)) {
getKeys(obj[key]);
} else {
result.push(key);
}
});
}
getKeys(data);
console.log(result)
我从嵌套的客户端得到 json,我想破坏它并制作子对象键,最好使用 underscore.js。
例如我的 json:
var data = {
or:[
{
dealershipCompany : 11
},
{
authType: 'google'
}],
and: [
{
or: [
{
firstName: {'contains': 'search'}
},
{
lastName: {'contains': 'search'}
},
{
email: {'contains': 'search'}
}]
}]
};
我想同时删除 'or' 和 'and'
当我使用 Object.keys(data) 获取对象键时,我得到
['0','1','2','3','4','5']
但我希望它是这样的
['dealershipCompany', 'authType', 'firstName', 'lastName','email']
我尝试了几次自己压平它,但总是对象键被编号
这里是jsFiddlelink
这应该有效:
var data = {
or:[
{ dealershipCompany : 11 },
{ authType: 'google' }
],
and: [ {
or: [
{ firstName: {'contains': 'search'} },
{ lastName: {'contains': 'search'} },
{ email: {'contains': 'search'} }
]
}]
};
function getOnlyObjects(data) {
var result = [];
if (Array.isArray(data)) {
data.forEach(function(item) {
result = result.concat(
getOnlyObjects(item)
);
});
}
else {
Object.keys(data).forEach(function(key) {
if (Array.isArray(data[key])) {
result = result.concat(
getOnlyObjects(data[key])
);
}
else {
result = result.concat(data);
}
});
}
return result;
}
function getData(data) {
return getOnlyObjects(data).map(function(item) {
return Object.keys(item)[0];
});
}
console.log(getData(data));
输出:
["dealershipCompany", "authType", "firstName", "lastName", "email"]
当你在数组上使用 Object.keys
时,你会得到索引,因此你得到 ['0','1','2','3','4','5']
.
编辑 1
已将 === 'and'
、=== 'or'
迁移到阵列 exceptionList
。您可以添加更多需要过滤的键。这将使过滤易于管理并保持清洁。
代码
var data = {
or: [{
dealershipCompany: 11
}, {
authType: 'google'
}],
and: [{
or: [{
firstName: {
'contains': 'search'
}
}, {
lastName: {
'contains': 'search'
}
}, {
email: {
'contains': 'search'
}
}, ]
}]
};
var result = [];
// You can add further keys that you want to filter
var exceptionList = ["and", "or"];
function getKeys(obj) {
var _keys = Object.keys(obj);
_keys.forEach(function(key) {
// Check if key is either,`and`, `or`, or an index of array.
if (exceptionList.indexOf(key) >=0 || !isNaN(key)) {
getKeys(obj[key]);
} else {
result.push(key);
}
});
}
getKeys(data);
console.log(result)