我有一个具有3级嵌套的JavaScript对象.我很难从第3级嵌套中获取值.
我已经对SO进行了一些研究并得到了基本的循环,但我似乎无法超越第一级.
这是我的代码
var customers = {
"cluster": [{
"id": "cluster1.1",
"color": "blue",
"flights": "784",
"profit": "524125",
"clv": "2364",
"segment": [{
"id": "segment1.1",
"color": "green",
"flights": "82",
"profit": "22150",
"clv": "1564",
"node": [{
"id": "node1.1",
"color": "orange",
"xpos": "1",
"ypos": "1"
}, {
"id": "node1.2",
"color": "orange",
"xpos": "1",
"ypos": "2"
}, {
"id": "node1.3",
"color": "orange",
"xpos": "1",
"ypos": "3"
}, {
"id": "node1.4",
"color": "orange",
"xpos": "1",
"ypos": "4"
}]
}, {
"id": "segment1.2",
"color": "red",
"flights": "2", …Run Code Online (Sandbox Code Playgroud) 我正在阅读'The Little Schemer',以便更好地理解编程的一些核心元素(即递归),并获得更多关于如何像程序员一样思考的想法.
这本书是作为入门级书籍推荐的,并且介绍说我需要知道的只有英语,数字和计数(我这样做).
我有点困惑,因为第一部分和问题的开头是问"这是原子吗?"
我错过了什么吗?我应该知道原子是什么吗?我很困惑,因为我认为这意味着更简单的英语.
蒂姆,提前谢谢
我正在为工作中的项目调试另一个开发人员的Javascript.
在一个美好的一天,我可能是一个中级Javascript开发人员,我遇到了一个看起来很糟糕的for循环:
for(i = 0; ; i++)
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我这是否确实是一个错误,或者在某些情况下,这是一个完全有效的方法来做高级的事情?
我试图在Windows Mobile 7.5上定位IE浏览器.任何人都可以告诉我,如果
<!--[if lt IE <mobile browser>]> <include retina display> <[end if]-->
Run Code Online (Sandbox Code Playgroud)
条件注释语法样式适用于Windows Mobile?
编辑:感谢下面的评论,我找到了一个解决方案.The <!--[if IEMobile]> <[end if]-->语法适用于Windows Mobile 7,但我无法使其适用于Windows Mobile 7.5.因为我构建不需要桌面设备上的我可以用一个通用的展示以及移动网站<!--[if gt IE 7]>评论说,围绕我的两次渲染之间遇到的问题得到.
如果那里的任何人有一个更优雅的解决方案,因为需要桌面支持而无法工作,我很乐意听到它.
我正在尝试(并且失败)编写一个正则表达式语句,在我的Javascript表单验证中检查特殊字符,例如!@#$%^&*()_ + <>?'"{} [].
据我所知,这可能已被问过1000次,但我受到了一些严重的时间压力.如果您不想回答下面的问题,并且您能够指出我前面回答上述问题的方向,我将非常感激.
在类似的说明中,任何人都可以告诉我为什么当我输入小写的'abc'等时出现以下错误?我很困惑.
jQuery.validator.addMethod("specialChars", function( value, element ) {
var regex = new RegExp("^[a-zA-Z0-9]+$");
var key = String.fromCharCode(event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
}, "please use only alphanumeric or alphabetic characters");
Run Code Online (Sandbox Code Playgroud) 如何使用Javascript / jQuery获取XML节点的属性值?
我正在尝试获取节点上的duration属性的值,然后获取fixedValue。
<loanAmount>
<interestRates>
<interestRate allowInterestOnly="true" type="variable" value="5.82"/>
<interestRate allowFixed="true" allowInterestOnly="true" duration="1" fixedInterestOnlyValue="5.7" fixedValue="5.7" variableInterestOnlyValue="5.82"/>
<interestRate allowFixed="true" allowInterestOnly="true" duration="3" fixedInterestOnlyValue="5.75" fixedValue="5.75" variableInterestOnlyValue="5.82"/>
<interestRate allowFixed="true" allowInterestOnly="true" duration="5" fixedInterestOnlyValue="6.64" fixedValue="6.56" variableInterestOnlyValue="5.82"/>
<interestRate allowFixed="true" allowInterestOnly="true" duration="10" variableInterestOnlyValue="5.82"/>
</interestRates>
</loanAmount>'
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经:
var currentLoanRates = function() {
var currLoanXml = '<loanAmount><interestRates><interestRate allowInterestOnly="true" type="variable" value="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="1" fixedInterestOnlyValue="5.7" fixedValue="5.7" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="3" fixedInterestOnlyValue="5.75" fixedValue="5.75" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="5" fixedInterestOnlyValue="6.64" fixedValue="6.56" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="10" variableInterestOnlyValue="5.82"/></interestRates></loanAmount>',
xmlDoc = $.parseXML( currLoanXml ),
$xml = …Run Code Online (Sandbox Code Playgroud) 我有一组JSON值,有3个级别:
cluster> segment> node
每个群集由段组成,每个段由节点组成.我试图弄清楚如何将其表示为JSON对象,我不确定如何创建结构.
每个节点都包含一个id以及对其segment id和cluster id的引用.我写了一个像这样的测试对象:
var customers = [
{
"cluster" :
{"flights":4, "profit":5245, "clv":2364,
"segment" :
{ "flights":2, "profit":2150, "clv":1564,
"node" :
{ 'xpos': 1, 'ypos': 2 }// closes node
}// closes segment
}//closes cluster
},
{
"cluster" :
{"flights":4, "profit":5245, "clv":2364,
"segment" :
{ "flights":2, "profit":2150, "clv":1564,
"node" :
{ 'xpos': 1, 'ypos': 2 }// closes node
}// closes segment
}//closes cluster
}
];
Run Code Online (Sandbox Code Playgroud)
感觉有点片状的部分是段和节点嵌套的方式.我没有收到任何错误,但这是表示此数据的最佳方式吗?
编辑:
感谢您的回答,它肯定指向了正确的方向,使用工具(jsonlint)并更好地理解json中的结构化数据.他们都是正确的答案,告诉我这是一个非常基本的问题.再次感谢.
我试图配置Apache使用.html文件而不是服务器端包含的.shtml文件.我已经读过在我的httpd.conf文件和.htaccess文件中使用"XBitHack On",但我不知道在哪里放这个.
我找到了这个信息:
"你需要设置每个需要解析的文件的执行位.这是通过Unix命令chmod + x fileName.html来完成的.这可以通过命令行的telnet/ssh连接或大多数FTP来完成.客户检查所有执行复选框."
我的问题是关于上面的引用:我在哪里运行这个命令?在服务器上?我在本地使用MAMP.
我已经阅读了一些相关的问题,但它们似乎与获得更基本的ssi设置有关(我的ssi正在使用.shtml扩展名) - 我的问题更多是关于使用ssi的.html文件.
谢谢
我在一些高级jQuery中看到了这种语法:
$container
Run Code Online (Sandbox Code Playgroud)
如:
$input = $container.find('.fn_this_timeline')
Run Code Online (Sandbox Code Playgroud)
编辑:基于下面的一些评论,只是为了澄清我能看到的'$ container'的第一个实例是在开头创建的:
init: function($container){
var myThing = this,
$input = $container.find('.fn_do_some_calc'),
$submit = $container.find('.fn_do_some_other_calc'),
defaultValue = parseInt($input.val(), 10);
myThing.$container = $container;
Run Code Online (Sandbox Code Playgroud)
以上这条线让我更加困惑:/
我习惯使用这样的jQuery选择器:$('#mySelector').method('etc');
有人能告诉我有什么区别,什么时候适合或适用于'速记'风格?
我知道这是非常基本的,但是如何获取我页面的body标签上的类名并将它们记录到控制台?我有以下内容:
if (navigator.userAgent.indexOf('OS 4') != -1) $("body").addClass('iOS4');
console.log($(body).attr(class).text());
Run Code Online (Sandbox Code Playgroud)
无论我的body标签是否具有'ios4'类,它都会注销到控制台吗?
我需要验证输入到表单中的数据是否在1940年1月1日之前.我分别有用户的日,月和年值,但我不知道如何使用智能将它们全部一起滚动并检查它们是否是在那个具体日期之前.
javascript ×7
jquery ×5
html ×3
.htaccess ×1
css ×1
date ×1
datetime ×1
for-loop ×1
json ×1
regex ×1
scheme ×1
ssi ×1
validation ×1
xml ×1
xml-parsing ×1