- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用对象键访问另一个对象中的一个对象。我正在连接一组 <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, '' )
关于Javascript:连接字符串中的额外字符阻止与对象键匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32802819/
我配置了我的RouteInitializer如下: class AppRouteInitializer implements RouteInitializer { init(Router rout
我正在尝试从 Android 应用程序发送短信。我正在使用 PendingIntent 以便我可以使用 Broadcast Receiver 检查它是否发送正常。由于 sendTextMessage
目录 简介 1 "额外"字段是什么 1.1 "额外"是指与业务无关 1.2 产生
应用程序读取 JSON 数据。然后它会将其放入 ListView (正确),但在按下某个项目后,我总是会得到显示的相同值。下面的代码我认为是问题所在,但我找不到。 try{ JSONArray
我正在使用以下代码 (Kotlin) 创建通知 val builder = NotificationCompat.Builder(ctx) ........ .set
我有一个问题。现在我正在使用 3 个面板,mainPanel 和其他 2 个面板(btnPanel 和 iconPanel)。所以问题是当我按下“重置”按钮时,我删除了 iconPanel 并再次添加
这是我的 html: Settings Export Import 和CSS: span.button { float:right; margin-righ
我正在尝试将一个结构编码为 JSON,然后将其插入我的 Mongo 数据库,但不断出现此错误:%!(EXTRA main.Test={575590180 Me})。我究竟做错了什么?我完全从我从事的另
嘿,我遇到了这些 latex 格式问题,有人可以提供一些帮助吗? .tex 文件: \begin{table}{} \renewcommand{\arraystretch}{1.1} \c
我在 FragmentPagerAdapter 中使用了 Fragment 的 ArrayList。 我想在 saveState() 中保存此 ArrayList 的状态,并在 restoreStat
我做了this MapKit-教程 一切正常,但如何为我的 pin 添加额外的属性? 这是我的课车: import Foundation import MapKit class Car: NSObje
关于 Android intent 将提供的附加功能有哪些文档? 更新: 我做了一些进一步的调查。我知道我们可以假设每个 Intent 都不会解析任何数据或额外内容,除非有明确记录。此外,一些(但不是
我在 python3.4.3 上使用 SqlAlchemy 来管理 MySQL 数据库。我正在创建一个表: from datetime import datetime from sqlalchemy
我正在使用 bootstrap 创建网页。我在两个 block (内容和标题)上派生了正文。在内容 block 中,我有 div 类 .container .sameTable 在里面我有 div 类
我在Windows 7上的MinGW和MSYS下使用gfortran构建了一些fortran程序。但是当我在未安装MinGW和MSYS的其他计算机上运行它们时,系统总是要求一些dll,例如libgfo
第一个元素的右侧似乎有额外的间距,我不知道它是从哪里来的。有人可以帮助我吗? 这是我使用的代码: http://jsfiddle.net/srabeat/tenx4y1c/1/ for (i = 0;
我使用 fs-extra 收到以下错误: ERROR { [Error: EPERM: operation not permitted, unlink 'C:\Projects\xxx\branche
我正在尝试在 CBC 模式下使用 AES-128 加密 320 字节的二进制数据,并将密码存储到一个文件中。输出文件应该是 320 字节,但我得到了 336 字节。这是我的代码: #include
我有一个特定的要求,我必须从我的 Activity 中触发浏览器上的 url。我可以使用以下代码执行此操作: Intent browserIntent = new Intent( Intent.A
我正在使用 JMS DI 注入(inject)带有注解的服务: use JMS\DiExtraBundle\Annotation as DI; /** * @DI\Service("foo.bar.
我是一名优秀的程序员,十分优秀!