Coffeeescript:比较两个嵌套数组与对象作为元素不起作用
Coffeeescript: Comparison of two nested arrays with objects as elements not working
Returns 错误,但期望为真:
verify = [1,2,3]
res = [
aktuell:
dp:1
aktuell:
dp:2
aktuell:
dp:3
]
arrayEqual = (a, b) ->
a.length is b.length and a.every (elem, i) -> elem is b[i].aktuell.dp
document.write arrayEqual verify, res
您的功能运行良好。您对对象的定义不是:
编译对象定义:
res = [
{
aktuell: {
dp: 1
},
aktuell: {
dp: 2
},
aktuell: {
dp: 3
}
}
]
这样,您将获得一个包含一个对象的数组,并且您的 every
部分不会被执行。
你必须使用大括号。
作品:
res =
[
aktuell: dp: 1
,
aktuell: dp: 2
,
aktuell: dp: 3
]
注意逗号的位置和意图。
编译为:
var res;
res = [
{
aktuell: {
dp: 1
}
}, {
aktuell: {
dp: 2
}
}, {
aktuell: {
dp: 3
}
}
];
对于 类 和对象:
myClass
count: ->
{
objectA:
objectA-2:['a-2-list']
objectB:
objectB-2:'b-2-string'
}
Returns 错误,但期望为真:
verify = [1,2,3]
res = [
aktuell:
dp:1
aktuell:
dp:2
aktuell:
dp:3
]
arrayEqual = (a, b) ->
a.length is b.length and a.every (elem, i) -> elem is b[i].aktuell.dp
document.write arrayEqual verify, res
您的功能运行良好。您对对象的定义不是:
编译对象定义:
res = [
{
aktuell: {
dp: 1
},
aktuell: {
dp: 2
},
aktuell: {
dp: 3
}
}
]
这样,您将获得一个包含一个对象的数组,并且您的 every
部分不会被执行。
你必须使用大括号。
作品:
res =
[
aktuell: dp: 1
,
aktuell: dp: 2
,
aktuell: dp: 3
]
注意逗号的位置和意图。
编译为:
var res;
res = [
{
aktuell: {
dp: 1
}
}, {
aktuell: {
dp: 2
}
}, {
aktuell: {
dp: 3
}
}
];
对于 类 和对象:
myClass
count: ->
{
objectA:
objectA-2:['a-2-list']
objectB:
objectB-2:'b-2-string'
}