因为我在map中有4个键/值,所以我尝试以字符串格式存储两个键,并将其余两个存储在一个数组中。
{ url: 'account/43',
status: '200',
headers: '\'content-type\' = \'application/json\'',
body: '{ name: Fatma Zaman }' }
预期输出:
{ url: 'account/43',
status: '200',
headers: [ '\'content-type\' = \'application/json\'' ],
body: [ '{ "name": "Fatma Zaman" }' ]}
下面是返回字符串中所有键/值对的代码,我可以将所有键/值对都放在数组中,但不确定如何只将两个键的值转换为数组。
function processFile(content) {
lodash.forEach(content, function(node) {
if (node.startsWith("//")) {
key = node.substring(2, node.length - 2).toLowerCase().trim()
return
} else {
value = node
}
map[key] = value
})
console.log(lodash.map(map, "key"))
return map
}
发布于 2018-06-03 08:31:05
您可以根据键名添加一个简单的条件,并将值放入数组中
映射键= 'header',‘body’。是否包含(键)?值: value;
function processFile(content) {
lodash.forEach(content, function(node) {
if (node.startsWith('//')) {
key = node
.substring(2, node.length - 2)
.toLowerCase()
.trim();
return;
} else {
value = node;
}
map[key] = ['header', 'body'].includes(key) ? [value] : value;
});
return map;
}
https://stackoverflow.com/questions/50662283
复制相似问题