给定一个唯一值列表,例如[5,4,9,2,1,7,'dog',9]有没有办法将其拆分为某个值?IE
[5,4,9,2,7,'dog'].split(4)
= [5,4],[9,2,7,'dog']
[5,4,9,2,7,'dog'].split(2)
= [5,4,9,2], [7,'dog']
Run Code Online (Sandbox Code Playgroud)
?
我正在写一个Fixnum类方法to_words,它接受任何数字并将其翻译成英语,所以,
2.to_words
#=> "two"
2030.to_words
#=> "two thousand thirty"
Run Code Online (Sandbox Code Playgroud)
我希望它能够处理所有数字,并且一旦我得到一点点超过10亿就会出现问题:
1000002000.to_words
#=> "one billion two thousand"
1074000000.to_words
#=> NoMethodError
1074000000.class
#=> Bignum
Run Code Online (Sandbox Code Playgroud)
有没有办法将我的Fixnum.to_words方法扩展到Bignum?
我刚来了accros这段代码:
var indx, hash;
loop:
for (var i in config.users) {
if (config.users[i].email === dataValues.email) {
indx = i;
hash = config.users[i].hash;
break loop;
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的代码吗?什么是"循环:"?谷歌'js循环'很难,而不仅仅是看到... in/while循环的常规
因此,以下两个函数打印出相同的精确结果.
console.log("i++");
for (i=1; i<=3; i++) {
console.log(i); // 1, 2
}
console.log("++i");
for (i=1; i<=3; ++i) {
console.log(i); // 1, 2
}
Run Code Online (Sandbox Code Playgroud)
这非常直观,因为我特别要求后增加一个并且每增加另一个.在for循环内运行之前增加值是非常理想的行为.这种行为是否一致?,这个javascript是特定的还是这是使用++ i,i ++语法和for循环的编程语言的标准行为?
我有一个名为的接口的导入 IFoo
interface IFoo {
name: string;
version: number;
init: (arg1: string, arg2: number) => Promise<string[]>;
}
Run Code Online (Sandbox Code Playgroud)
我所关心的init是有没有办法提取它的类型,以便我可以在其他地方使用这个接口?IE。
inteface ICustom {
properties: {
group: string,
init: IFoo.init
}
amount: number
}
Run Code Online (Sandbox Code Playgroud) 我的渲染看起来像这样:
<ul>{liElements}</ul>
Run Code Online (Sandbox Code Playgroud)
这liElements是一个<li>word</li>用for-of循环创建的数组.
(为了参数,我无法访问创建该数组的函数)
我理解键的价值,但是在这种情况下它们没有价值,因为组件的值都不会改变,特别是因为没有键,"反应将阵列位置作为关键",无论如何,完美!我想要那个,为什么它坚持要给我发一条错误信息让我知道它们是必要的,我知道它不知道它们没有必要,不管它也不知道它们是必要的,所以怎么回事黄色警告?
我知道我可以忽略它,但总是将消息日志安装到控制台是非常烦人的(特别是因为这个消息在其他地方很有用)
react.js:19287 Warning: Each child in an array or iterator should have a unique "key" prop
Run Code Online (Sandbox Code Playgroud)
所以,因为我能做到
liElements[0].key // null
Run Code Online (Sandbox Code Playgroud)
我以为我可以做到
liElements.map(function(a,i){a.key=i;});
<ul>{liElements}</ul>
Run Code Online (Sandbox Code Playgroud)
唉;
bundle.js:39123 Uncaught TypeError: Cannot assign to read only property 'key' of object '#<Object>'
Run Code Online (Sandbox Code Playgroud)
为什么只读?无论如何,我收到数组后,我可以分配这些完全不必要的密钥,以便错误信息停止显示?
任何人都可以解释这里发生了什么?
> var b = { a: '1' }
> b['a']
'1'
> { a:'1' }['a']
[ 'a' ]
> {}['a']
[ 'a' ]
> {c:2}['a']
[ 'a' ]
and of course
> ({ a:1 })['a']
1
Run Code Online (Sandbox Code Playgroud)
但我也希望{a:1} ['a']返回1或错误,为什么我得到['a']?
javascript ×4
class-method ×1
for-loop ×1
inheritance ×1
list ×1
loops ×1
python ×1
reactjs ×1
ruby ×1
syntax ×1
typescript ×1