admin管理员组文章数量:1297014
I've methods on SO for flattening an array javascript, i.e. converting [1,[2,3]]
into [1,2,3]
. But I'm looking for a concise method of flattening a dictionary while preserving the keys. Specifically, I want a dictionary that looks like this:
{'key1':1,'key2':{'key3':2,'key4':3,'key5':{'key6':4}}}
To be converted into:
{'key1':1, 'key2.key3':2,'key2.key4':3,'key2.key5.key6':4}
The exact output format (dictionary, list of pairs, etc) isn't important, as long as it clearly associates the string of nested keys with a value. For my purposes, it's also OK to assume that none of the keys contain a .
character, so that it can be used to denote the next key.
I've methods on SO for flattening an array javascript, i.e. converting [1,[2,3]]
into [1,2,3]
. But I'm looking for a concise method of flattening a dictionary while preserving the keys. Specifically, I want a dictionary that looks like this:
{'key1':1,'key2':{'key3':2,'key4':3,'key5':{'key6':4}}}
To be converted into:
{'key1':1, 'key2.key3':2,'key2.key4':3,'key2.key5.key6':4}
The exact output format (dictionary, list of pairs, etc) isn't important, as long as it clearly associates the string of nested keys with a value. For my purposes, it's also OK to assume that none of the keys contain a .
character, so that it can be used to denote the next key.
- underscorejs has some useful functions for manipulating data structures. – js1568 Commented Feb 26, 2014 at 16:24
4 Answers
Reset to default 5var keys = {'key1':1,'key2':{'key3':2,'key4':3,'key5':{'key6':4}}};
var result = {};
function serialize(keys, parentKey){
for(var key in keys){
if(parseInt(keys[key], 10)){
result[parentKey+key] = keys[key];
}else{
serialize(keys[key], parentKey+key+".");
}
}
}
serialize(keys, "");
console.log(result);
Hope this is what you want:
{ key1: 1, 'key2.key3': 2, 'key2.key4': 3, 'key2.key5.key6': 4 }
Here is another approach to the problem.
var myDict = {'key1':1,'key2':{'key3':2,'key4':3,'key5':{'key6':4}}};
function flattenDict(dictToFlatten) {
function flatten(dict, parent) {
var keys = [];
var values = [];
for(var key in dict) {
if(typeof dict[key] === 'object') {
var result = flatten(dict[key], parent ? parent + '_' + key : key);
keys = keys.concat(result.keys);
values = values.concat(result.values);
}
else {
keys.push(parent ? parent + '_' + key : key);
values.push(dict[key]);
}
}
return {
keys : keys,
values : values
}
}
var result = flatten(dictToFlatten);
var flatDict = {};
for(var i = 0, end = result.keys.length; i < end; i++) {
flatDict[result.keys[i]] = result.values[i];
}
return flatDict;
}
flattenDict(myDict);
This code does not use global variable for storing the result.
var dict = {'key1':1,'key2':{'key3':2,'key4':3,'key5':{'key6':4}}};
function flatten(obj, suffix, ans) {
for (var x in obj) {
var key;
if (suffix != '')
key = suffix + '.' + x;
else
key = x;
if (typeof obj[x] === 'object') {
flatten(obj[x], key, ans);
} else {
ans[key] = obj[x];
}
}
}
var x = {};
flatten(dict, "", x)
console.log(x)
This is a modified version of @SPS where I take in account a scenario like: {"a": {"b": {"": "Test"} } };
// {"a": {"b": {"": "Test"} } };
function flatten(dict) {
let res = {}
helper(dict, "", res);
return res;
}
function helper(obj, suffix, ans) {
for (let k in obj) {
let key;
if (suffix !== '' && k !== '') {
key = `${suffix}.${k}`;
} else if (suffix !== '' && k === '') {
key = suffix;
} else {
key = k;
}
if (typeof obj[k] === 'object') {
helper(obj[k], key, ans);
} else {
ans[key] = obj[k];
}
}
}
本文标签: Flattening a Javascript dictionarypreserving the nested keysStack Overflow
版权声明:本文标题:Flattening a Javascript dictionary, preserving the nested keys - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741646740a2390227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论