使用MomentJS,如果需要,我可以使用.locale(或.lang在旧版本中)在特定时刻实例(而不是全局)上设置区域设置.如何使用除全局区域之外的特定区域设置解析字符串?由于moment函数本身就是我们用于解析的东西,并且似乎.set没有完整解析的变体?
例如:
// Assume the default locale is 'en' (just in case, I'll set it for the snippet)
moment.locale('en');
// I have this string that's in the French locale
var str = "30 Avril 2015";
// Parsing it fails
var m = moment(str, "DD MMMM YYYY");
snippet.log(m.format("YYYY-MM-DD")); // "Invalid Date"Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>Run Code Online (Sandbox Code Playgroud)
我从api xml数据获取,其中一个xml元素是日期时间,nodeValue始终采用这种格式 - 字符串:"YYYY-MM-DD".(我无法从api请求,以不同格式返回日期时间)
我的问题是将此格式拆分并转换为此字符串:"DD.MM.YYYY"
基本上我这样做了:
var myString = "2015-04-10"; //xml nodeValue from time element
var array = new Array();
//split string and store it into array
array = myString.split('-');
//from array concatenate into new date string format: "DD.MM.YYYY"
var newDate = (array[2] + "." + array[1] + "." + array[0]);
console.log(newDate);
Run Code Online (Sandbox Code Playgroud)
这是jsfiddle:http://jsfiddle.net/wyxvbywf/
现在,这段代码可行,但我的问题是:有没有办法以更少的步骤获得相同的结果?
var metricUnits = function(num) {
console.log(num.toString().length);
};
// This works, gives me 21
metricUnits(900000000000000000000);
// But this fails, gives me 5
metricUnits(9000000000000000000000);
Run Code Online (Sandbox Code Playgroud)
当我调用此函数时,21将记录到控制台.但是,当我在输入参数的末尾添加一个或多个零时,将5打印到控制台?!为什么是这样?
我读到只做& 0x7fffffff符号位的掩码,并没有篡改其他位.
int a = Integer.MIN_VALUE;
System.out.println(a & 0x7fffffff);
Run Code Online (Sandbox Code Playgroud)
但是,这段代码输出
0
Run Code Online (Sandbox Code Playgroud)
代替
2147483648
Run Code Online (Sandbox Code Playgroud)
这是为什么?
一个PHP脚本正在计算我以下数组:
$test = Array();
$test['a'] = Array();
$test['a']['a1'] = 'a1';
$test['a']['a2'] = 'a2';
$test['b'] = Array();
$test['b']['b1'] = 'b1';
$test['b']['b2'] = 'b2';
Run Code Online (Sandbox Code Playgroud)
我正在使用以下方法将此数组转换为JSON:
echo json_encode($test);
Run Code Online (Sandbox Code Playgroud)
我正在使用Ajax调用检索此JSON,我正在使用以下方法将其转换为JavaScript数组:
test = JSON.parse(data);
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何在JavaScript中向此数组添加条目?我试过了:
test['c'] = [];
test['c']['c1'] = 'c1';
test['c']['c2'] = 'c2';
Run Code Online (Sandbox Code Playgroud)
但是然后在控制台中test['c']是空的(Array[0]).
下面的脚本将替换实际图像对象的所有图像标记.不幸的是,我收到了一个Uncaught TypeError错误.这是因为在我的示例中,Javascript从0计数到3,而不是1到4.数组的长度为4.
如何避免此错误(因此我也在while循环后获得控制台日志)?
var raw_content = {
"text" : "Test image 1 [image] Test image 2 [image] Test image 3 [image] Test image 4 [image]",
"media" :[
{
"src": "http://placehold.it/400x200",
"caption": "This is a caption"
},{
"src": "images/gallery/sh0.png",
"caption": "This is a caption"
},{
"src": "http://placehold.it/400x200",
"caption": "This is a caption"
},{
"src": "images/gallery/sh0.png",
"caption": "This is a caption"
}
]
};
// find img one by one
var image_counter = 0;
var text_buffer = raw_content.text; …Run Code Online (Sandbox Code Playgroud) 我在nodejs上运行此代码.我想知道为什么闭包执行时不打印字符串'Globals'?是不是this在关闭指向全球范围?
// Running on NodeJS, not in a browser!
this.name = "Globals";
function Person(name) {
this.name = name;
this.namePrinter = function() {
return function() {
console.log(this.name);
}
}
}
var p = new Person("Faiz");
p.namePrinter()(); // prints undefined. Shouldn't it print Globals?
console.log(this.name); // prints Globals
Run Code Online (Sandbox Code Playgroud) 有什么区别
import { pick } from 'lodash';
Run Code Online (Sandbox Code Playgroud)
和
import pick from 'lodash/pick';
Run Code Online (Sandbox Code Playgroud)
(注意它'lodash/pick'在第二个,而不仅仅是'lodash'.)
它们如何影响束大小?
他们进口完全相同的部分lodash吗?
他们比较快吗?
好的,所以我知道float是32位十进制。Double是64位十进制。长为64位整数。那么为什么要使用该值:Float(32位)<-Long(64位)。显然不是Java代码,但是我在Java中引用它。即使将其压缩为32位并失去精度,它也会自动进行转换。因此,这意味着精度无关紧要。但是,除非将double显式转换为int,否则int <-double无效。它的大小较小,它们既是数字又是基元。只是一般规则:十进制数<-整数,与位大小无关。它将循环返回。只是这个规则,一切都可以在Java中的Numbers中工作?显示实际代码:
long L = 10_000L;
float f = L;
Run Code Online (Sandbox Code Playgroud)
请注意,问题是专门针对没有显式强制转换的问题long,float而不是float针对long,而不是有关显式强制转换的问题。
我知道这个问题已经在stackoverflow上被问了很多,我已经搜索了很多,但仍然无法理解。
async function testFunc() {
var test = await getSomething();
//test.resolve();
console.log("hello" + test);
return "";
}
testFunc().then(token => {}).catch(x => {});
function getSomething() {
return "ex";
}Run Code Online (Sandbox Code Playgroud)
在大多数答案中,建议使用.then()来解决诺言,但我已经做到了,但我仍未完成诺言。这有什么问题?
在https://repl.it/repls/UntrueLankySorting上进行了测试
它向我展示了这一点:
你好
=>承诺{<pending>}
javascript ×8
java ×2
arrays ×1
asynchronous ×1
bit ×1
casting ×1
closures ×1
ecmascript-6 ×1
import ×1
lodash ×1
long-integer ×1
momentjs ×1
node.js ×1
operations ×1
php ×1
promise ×1
scope ×1
split ×1
string ×1
webpack ×1