在JavaScript中验证十进制数的最干净,最有效的方法是什么?
奖励积分:
测试用例:
01. IsNumeric('-1') => true
02. IsNumeric('-1.5') => true
03. IsNumeric('0') => true
04. IsNumeric('0.42') => true
05. IsNumeric('.42') => true
06. IsNumeric('99,999') => false
07. IsNumeric('0x89f') => false
08. IsNumeric('#abcdef') => false
09. IsNumeric('1.2.3') => false
10. IsNumeric('') => false
11. IsNumeric('blah') => false
Run Code Online (Sandbox Code Playgroud) 为什么有人更喜欢lodash.js或underscore.js实用程序库而不是另一个?
Lodash似乎是下划线的替代品,后者已经存在了更长时间.
我认为两者都很精彩,但我对他们如何努力进行有根据的比较并不了解,我想更多地了解这些差异.
如何检查var是否是JavaScript中的字符串?
我试过这个但它不起作用......
var a_string = "Hello, I'm a string.";
if (a_string typeof 'string') {
// this is a string
}
Run Code Online (Sandbox Code Playgroud) 我的Vue组件是这样的:
<template>
<div>
<div class="panel-group"v-for="item in list">
<div class="col-md-8">
<small>
Total: <b>{{ item.total }}</b>
</small>
</div>
</div>
</div>
</template>
<script>
export default {
...
computed: {
list: function() {
return this.$store.state.transaction.list
},
...
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
结果{{ item.total }}是
2600
但我希望格式化如下:
26.000.000,00
在jquery或javascript中,我可以做到
但是,如何在vue组件中执行此操作?
如何检查变量是数字,整数还是字符串数字?
在PHP中,我可以这样做:
if (is_int($var)) {
echo '$var is integer';
}
Run Code Online (Sandbox Code Playgroud)
要么:
if (is_numeric($var)) {
echo '$var is numeric';
}
Run Code Online (Sandbox Code Playgroud)
我怎么能在jQuery/JavaScript中做到这一点?
我有一个HTML表,想要$scope.records通过单击表头($scope.headers在ctrl中)对我的记录(在ctrl中)进行排序,
任何人都可以解释为什么这样做:
<th>
<a ng-click="sortColumn=headers[0];reverse=!reverse">{{ headers[0] }}</a>
</th>
<th>
<a ng-click="sortColumn=headers[1];reverse=!reverse">{{ headers[1] }}</a>
</th>
Run Code Online (Sandbox Code Playgroud)
那不是:
<th ng-repeat="header in headers">
<a ng-click="sortColumn=headers[$index];reverse=!reverse">{{ headers[$index] }}</a>
</th>
Run Code Online (Sandbox Code Playgroud)
以下是记录的代码:
<tr ng-repeat="arr in records | orderBy:sortColumn:reverse">
<td ng-repeat="val in arr" ng-bind-html-unsafe="arr[headers[$index]]</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我的表中有58列,所以循环表头更好...
我正在尝试对从JSON源填充的数据表进行排序.我的代码如下:
HTML:
<div ng-app="myApp">
<div ng-controller="PurchasesCtrl">
<table cellspacing="0">
<tr class="first">
<th class="first" ng:click="changeSorting(purchases.date)">Date</th>
<th ng:click="changeSorting(purchases.text)">Description</th>
<th ng:click="changeSorting(purchases.price)">Amount</th>
<th ng:click="changeSorting(purchases.availability)">Status</th>
</tr>
<tr ng-repeat="purchase in purchases.data">
<td class="first">{{purchase.date}}</td>
<td>{{purchase.text}}</td>
<td>{{purchase.price}}</td>
<td>{{purchase.availability}}</td>
</tr>
</table>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
var myApp = angular.module("myApp",[]);
myApp.factory("Purchases", function(){
var Purchases = {};
Purchases.data = [
{
date: "10/05/2012",
text: "1 Lorem ipsum dolor sit amet ipsum dolor",
price: "£123.45",
availability: "1 Available until 10th Dec 2013"
},
{
date: "24/05/2012",
text: "2 Lorem ipsum dolor sit amet ipsum …Run Code Online (Sandbox Code Playgroud) <script type="text/javascript">
function saveName (firstName) {
function capitalizeName () {
return firstName.toUpperCase();
}
var capitalized = capitalizeName();console.log(capitalized instanceof String);
return capitalized;
}
console.log(saveName("Robert")); // Returns "ROBERT"
</script>
Run Code Online (Sandbox Code Playgroud)
题:
我想检查大写的类型,所以我使用capitalized instanceof String?但它显示:false在控制台中,我不想尝试capitalized instanceof Function,Object...这将花费太多时间,那么检测变量类型的最佳方法是什么?
我需要比较来自 xml 标记属性字段的两个十六进制值,我正在尝试这个:
var fill = $(this).attr( "fill" );
// console.log( fill.toString(16) );
if ( fill === "#FF00FF" )
Run Code Online (Sandbox Code Playgroud)
但是没有任何想法吗?
这个问题是[]的一个衍生产品,是Array的一个实例,但""不是String的实例
鉴于
"" instanceof String; /* false */
String() instanceof String; /* false */
new String() instanceof String; /* true */
Run Code Online (Sandbox Code Playgroud)
和
typeof "" === "string"; /* true */
typeof String() === "string"; /* true */
typeof new String() === "string"; /* false */
Run Code Online (Sandbox Code Playgroud)
然后,如果我有一个变量abc,我想知道它是否是一个字符串,我可以做
if(typeof abc === "string" || abc instanceof String){
// do something
}
Run Code Online (Sandbox Code Playgroud)
有没有更简单,更短和本地的方式,或者我必须创建自己的功能?
function isStr(s){
return typeof s === "string" || s instanceof String;
}
if(isStr(abc)){
// do something
}
Run Code Online (Sandbox Code Playgroud) 我想检查类型文本输入的值是数字还是字符串,如下所示:
<input
type="text"
onChange={(e) => {
const value = e.target.value;
console.log(typeof value);
}}
/>
Run Code Online (Sandbox Code Playgroud)
但是,当我输入文本或数字时,控制台会在这两种情况下记录字符串。
如何检查react js中输入类型文本的值是否为数字?
javascript ×10
angularjs ×2
jquery ×2
string ×2
validation ×2
html-table ×1
input ×1
lodash ×1
numbers ×1
reactjs ×1
sorting ×1
vue.js ×1
vuejs2 ×1
vuex ×1