所以我得到了以下javascript,其中包含一个键/值对,用于将嵌套路径映射到目录。
function createPaths(aliases, propName, path) {
aliases.set(propName, path);
}
map = new Map();
createPaths(map, 'paths.aliases.server.entry', 'src/test');
createPaths(map, 'paths.aliases.dist.entry', 'dist/test');
现在我想要做的是从地图中的键创建一个JSON对象。
必须是,
paths: {
aliases: {
server: {
entry: 'src/test'
},
dist: {
entry: 'dist/test'
}
}
}
不确定是否有一种方法可以做到这一点。任何帮助表示赞赏。
答案 0 :(得分:4)
您可以循环遍历地图和键,并指定值
function createPaths(aliases, propName, path) {
aliases.set(propName, path);
}
var map = new Map(),
object = {};
createPaths(map, 'paths.aliases.server.entry', 'src/test');
createPaths(map, 'paths.aliases.dist.entry', 'dist/test');
map.forEach((value, key) => {
var keys = key.split('.'),
last = keys.pop();
keys.reduce((r, a) => r[a] = r[a] || {}, object)[last] = value;
});
console.log(object);
答案 1 :(得分:3)
我希望这个功能足够自我解释。这就是我以前做的工作。
/*
* Turn the map<String, Object> to an Object so it can be converted to JSON
*/
function mapToObj(inputMap) {
let obj = {};
inputMap.forEach(function(value, key){
obj[key] = value
});
return obj;
}
JSON.stringify(returnedObject)
答案 2 :(得分:2)
另一种方法。我很好奇哪个有更好的表现,但jsPerf失败了:(。
var obj = {};
function createPaths(map, path, value)
{
if(typeof path === "string") path = path.split(".");
if(path.length == 1)
{
map[path[0]] = value;
return;
}
else
{
if(!(path[0] in map)) map[path[0]] = {};
return createPaths(map[path[0]], path.slice(1), value);
}
}
createPaths(obj, 'paths.aliases.server.entry', 'src/test');
createPaths(obj, 'paths.aliases.dist.entry', 'dist/test');
console.log(obj);
没有递归:
var obj = {};
function createPaths(map, path, value)
{
var map = map;
var path = path.split(".");
for(var i = 0, numPath = path.length - 1; i < numPath; ++i)
{
if(!(path[i] in map)) map[path[i]] = {};
map = map[path[i]];
}
map[path[i]] = value;
}
createPaths(obj, 'paths.aliases.server.entry', 'src/test');
createPaths(obj, 'paths.aliases.dist.entry', 'dist/test');
createPaths(obj, 'paths.aliases.dist.dingo', 'dist/test');
createPaths(obj, 'paths.bingo.dist.entry', 'dist/test');
console.log(obj);
var obj = {};
function createPaths(map, path, value)
{
var map = map;
var path = path.split(".");
while(path.length > 1)
{
map = map[path[0]] = map[path.shift()] || {};
}
map[path.shift()] = value;
}
createPaths(obj, 'paths.aliases.server.entry', 'src/test');
createPaths(obj, 'paths.aliases.dist.entry', 'dist/test');
createPaths(obj, 'paths.aliases.dist.dingo', 'dist/test');
createPaths(obj, 'paths.bingo.dist.entry', 'dist/test');
console.log(obj);
答案 3 :(得分:2)
Object.fromEntries
const log = console.log;
const map = new Map();
// undefined
map.set(`a`, 1);
// Map(1) {"a" => 1}
map.set(`b`, 2);
// Map(1) {"a" => 1, "b" => 2}
map.set(`c`, 3);
// Map(2) {"a" => 1, "b" => 2, "c" => 3}
// Object.fromEntries ✅
const obj = Object.fromEntries(map);
log(`\nobj`, obj);
// obj { a: 1, b: 2, c: 3 }
...spread
和destructuring assignment
const log = console.log;
const map = new Map();
// undefined
map.set(`a`, 1);
// Map(1) {"a" => 1}
map.set(`b`, 2);
// Map(1) {"a" => 1, "b" => 2}
map.set(`c`, 3);
// Map(2) {"a" => 1, "b" => 2, "c" => 3}
const autoConvertMapToObject = (map) => {
const obj = {};
for (const item of [...map]) {
const [
key,
value
] = item;
obj[key] = value;
}
return obj;
}
const obj = autoConvertMapToObject(map)
log(`\nobj`, obj);
// obj { a: 1, b: 2, c: 3 }
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
答案 4 :(得分:1)
鉴于{v3},fromEntries()
自节点v12开始可用:
const entries = new Map([
['foo', 'bar'],
['baz', 42]
]);
const obj = Object.fromEntries(entries);
console.log(obj);
// expected output: Object { foo: "bar", baz: 42 }