Javascript TypeError:映射[键]。推送不是函数
我不确定为什么会出现此错误,因为我的函数中确实有map变量declare:-( 如果我运行以下代码:Javascript TypeError:映射[键]。推送不是函数,javascript,node.js,Javascript,Node.js,我不确定为什么会出现此错误,因为我的函数中确实有map变量declare:-( 如果我运行以下代码: if (key in map) { map[key].push(value) } else { map[key] = value } 我的输出如下: { url: ['account/43' ], status: [ '200' ], headers: [ 'content-ty
if (key in map) {
map[key].push(value)
} else {
map[key] = value
}
我的输出如下:
{ url: ['account/43' ],
status: [ '200' ],
headers:
[ 'content-type = application/json',
'content-type = application/text' ],
body: [ '{ name: xyz}' ] }
如果我在函数内的代码行下方运行,则会出现以下情况:
map[key] = ["headers", "body"].includes(key)? [value] : value
输出如下图所示(url/status为字符串,headers/body为数组格式),但它不接受标题的多个值,基本上是替换值
{ url: 'account/43',
status: '200',
headers: [ 'content-type = application/text' ],
body: [ '{ name: xyz }' ] }
我正在尝试实现这两种情况(首先,url.status应为字符串格式,headers/body应为数组格式。其次,headers或body可以附加/推送多个值,如以下输出:
{url: 'account/43',
status: '200',
headers:
[ 'content-type = application/json',
'content-type = application/text' ],
body: [ '{ name: xyz }' ] }
下面是实际的函数
function processFile(content) {
let map = {}
content.forEach(function(node) {
if (node.startsWith("//")) {
key = node.substring(2, node.length-2).toLowerCase().trim()
return
} else {
value = node
}
if (key in map) {
map[key].push(value)
} else {
map[key] = value
}
map[key] = ["headers", "body"].includes(key)? [value] : value
})
return map
}
错误
这就是你能做到的-
函数进程文件(内容){
设map={}
content.forEach(函数(节点){
if(node.startsWith(“/”){
key=node.substring(2,node.length-2).toLowerCase().trim()
返回
}否则{
值=节点
}
如果(输入地图){
映射[键]。推送(值)
}否则{
映射[键]=[值];
}
映射[键]=[标题”,“正文]。包括(键)?[值]:值
})
返回图
}
您已确保对于标题
和正文
,可以使用多个值。但是,根据错误,很明显,某些其他键也存在重复
考虑使用如下函数:
function processFile(content) {
let key;
return content.reduce(
function (map, node) {
if (node.startsWith('//')) {
key = node.substring(2, node.length-2).toLowerCase().trim();
} else if (key) {
if (map[key]) {
if (map[key].push) {
map[key].push(node);
} else {
throw new Error(`Duplicate key ${key} for scalar value`);
}
} else {
map[key] = node;
}
} else {
throw new Error(`Data encountered without a key: ${node}`);
}
return map;
},
{ headers: [], body: [] }
);
}
map[key] = [value] // array containing value in the first position
这将在遇到重复密钥时引发错误,使您有机会对其进行调试
我所作的其他改进:
- 使用
代替reduce()
forEach()
- 使用
和标题
的空数组初始化映射,这样就不需要在迭代函数中进行特殊情况测试正文
- 声明的
,该键以前不必要是全局键键
- 没有先前看到的键的数据会导致错误,而不是将数据存储在
中map[“undefined”]
map[key] = value
后者称:
map[key].push(value)
map[key]
中没有数组。为了将元素推送到这里,我们需要在map[key]
中存储数组。要这样做,而不是
map[key] = value //element value being stored in map[key]
我们需要这样做:
function processFile(content) {
let key;
return content.reduce(
function (map, node) {
if (node.startsWith('//')) {
key = node.substring(2, node.length-2).toLowerCase().trim();
} else if (key) {
if (map[key]) {
if (map[key].push) {
map[key].push(node);
} else {
throw new Error(`Duplicate key ${key} for scalar value`);
}
} else {
map[key] = node;
}
} else {
throw new Error(`Data encountered without a key: ${node}`);
}
return map;
},
{ headers: [], body: [] }
);
}
map[key] = [value] // array containing value in the first position
初始化并存储数组。
否则{map[key]=[value]}
,您必须将映射中的key
指定为数组,以便在下一次迭代中推送值,其中key
甚至是声明的!看起来发生的事情是,您遇到了url或status的多个值,并且您基本上确保了这些值只能是单个值。