gpt4 book ai didi

Javascript:连接字符串中的额外字符阻止与对象键匹配

转载 作者:行者123 更新时间:2023-12-01 17:51:13 24 4
gpt4 key购买 nike

我正在尝试使用对象键访问另一个对象中的一个对象。我正在连接一组 <select> 的值元素来构建与键匹配的字符串。不幸的是,这永远行不通。下面是代码:

    var valueKeyString = "";
$(".keySelector").each(function(){
valueKeyString += $(this).val();
});
if (geoAttrs[selectedGeoAttr].rowCol == "row") {
mapData = rowValueGroups[valueKeyString];
} else if (geoAttrs[selectedGeoAttr].rowCol == "col") {
mapData = colValueGroups[valueKeyString];
}

在尝试了很多之后,我使用 charCodeAt() 逐个字符地测试了字符串:

    var test1 = valueKeyString;
for (var i = 0, len = valueKeyString.length; i < len; i++) {
test1 += valueKeyString.charCodeAt(i)+" ";
}
if (geoAttrs[selectedGeoAttr].rowCol == "row") {
Object.keys(colValueGroups).forEach(function(group) {
var test2 = group;
for (var i = 0, len = group.length; i < len; i++) {
test2 += group.charCodeAt(i)+" ";
}
console.log(test1);
console.log(test2)
})

mapData = colValueGroups[valueKeyString];
}

我的连接字符串在连接点都有一个 charCode 为 0 的额外字符。无法弄清楚为什么它在那里或如何使用正则表达式或 str.replace() 摆脱它.最终得到了一个丑陋但实用的解决方案,我只是测试对象键以查看它们是否包含来自 <select> 的值。元素:

    var valueKeys = [];
$(".keySelector").each(function(){
valueKeys.push($(this).val());
});
if (geoAttrs[selectedGeoAttr].rowCol == "row") {
Object.keys(colValueGroups).forEach(function(group) {
var groupHasAllKeys = true;
valueKeys.forEach(function(key) {
if (group.indexOf(key) == -1 ) {
groupHasAllKeys = false;
}
});
if(groupHasAllKeys) {
mapData = colValueGroups[group];
}
});
} else if (geoAttrs[selectedGeoAttr].rowCol == "col") {
Object.keys(rowValueGroups).forEach(function(group) {
var groupHasAllKeys = true;
valueKeys.forEach(function(key) {
if (group.indexOf(key) == -1 ) {
groupHasAllKeys = false;
}
});
if(groupHasAllKeys) {
mapData = rowValueGroups[group];
}
});
}

一定有更好的方法,对吧?这到底是怎么回事?

编辑:rowValueGroups可能看起来像这样:

{
"35_to_44_yearsMale": {
"4654387684": {
"value": 215
},
"4654387685": {
"value": 175
},
"4654387686": {
"value": 687
},
"4654387687": {
"value": 172
}
},
"45_to_54_yearsMale": {
"4654387684": {
"value": 516
},
"4654387685": {
"value": 223
},
"4654387686": {
"value": 54
},
"4654387687": {
"value": 164
}
}
}

valueKeyString应该是 "45_to_54_yearsMale"等等。

这是调查数据。我正在提取 rowValueGroups来自 nicolaskruchten/pivottable 自定义渲染器输出的数据。 (特别是来自 pivotData.tree 对象,如果你好奇的话)。我不打算改变数据透视表的核心来改变这些键的格式,所以我只是想我会连接一个 select 元素的几个值以使其工作。

以下是上述测试的部分控制台输出:

35_to_44_yearsMale51 53 95 116 111 95 52 52 95 121 101 97 114 115 0 77 97 108 101
35_to_44_yearsMale51 53 95 116 111 95 52 52 95 121 101 97 114 115 77 97 108 101

第一行是对 valueKeyString 进行的测试,第二行是对 rowValueGroups 中的一个键进行的测试.请注意,初始字符串看起来相同(对于 valueKeyString,Firefox 控制台实际上在“years”和“Male”之间输出了一个未找到的小方 block ),但 charCodeAt()在连接点出现那个奇怪的 0 字符。

最佳答案

您的字符串中有 null 个字符。此正则表达式将删除它们:

valueKeyString = valueKeyString.replace( /\0/g, '' )

引用:Removing null characters from a string in JavaScript

关于Javascript:连接字符串中的额外字符阻止与对象键匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32802819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com