if(value instanceof number)
时间: 2024-01-10 09:05:01 浏览: 25
如果你想检查一个值是否是一个数字类型,你可以使用 `typeof` 操作符来判断。例如:
```javascript
if (typeof value === 'number') {
// 值是一个数字
} else {
// 值不是一个数字
}
```
请注意,`typeof` 操作符返回的是一个字符串,表示值的类型。对于数字类型,它返回的是 `'number'`。
相关问题
/** * 递归函数 * @param setValues 树型数据 * @param valueId 查找的id */ function recursionGet(setValues, valueId) { let value = null; for (let index = 0; index < setValues.length; index += 1) { if (setValues[index].value === valueId) { value = setValues[index].label; break; } if (setValues[index].children instanceof Array && setValues[index].children.length > 0) { const text = recursionGet(setValues[index].children, valueId); if (text) return text; } } return value; } // 调用 const data =recursionGet(list,301) console.log(data) // 输出 行政 用vue写出来
<template>
<div>
<span>{{ department }}</span>
</div>
</template>
<script>
export default {
name: "DepartmentSelector",
props: {
list: {
type: Array,
required: true
},
valueId: {
type: Number,
required: true
}
},
data() {
return {
department: null
};
},
mounted() {
this.department = this.recursionGet(this.list, this.valueId);
},
methods: {
recursionGet(setValues, valueId) {
let value = null;
for (let index = 0; index < setValues.length; index += 1) {
if (setValues[index].value === valueId) {
value = setValues[index].label;
break;
}
if (
setValues[index].children instanceof Array &&
setValues[index].children.length > 0
) {
const text = this.recursionGet(
setValues[index].children,
valueId
);
if (text) return text;
}
}
return value;
}
}
};
</script>
重写map的put方法让相同key的value相加
可以通过继承AbstractMap类并重写put方法来实现相同key的value相加的功能。具体实现如下:
```java
import java.util.AbstractMap;
import java.util.Map;
public class SummingMap<K, V extends Number> extends AbstractMap<K, V> {
private final Map<K, V> map;
public SummingMap(Map<K, V> map) {
this.map = map;
}
@Override
public V put(K key, V value) {
V oldValue = map.get(key);
if (oldValue == null) {
return map.put(key, value);
}
V newValue = sum(oldValue, value);
return map.put(key, newValue);
}
private V sum(V oldValue, V value) {
if (oldValue instanceof Double || value instanceof Double) {
return (V) Double.valueOf(oldValue.doubleValue() + value.doubleValue());
} else if (oldValue instanceof Float || value instanceof Float) {
return (V) Float.valueOf(oldValue.floatValue() + value.floatValue());
} else if (oldValue instanceof Long || value instanceof Long) {
return (V) Long.valueOf(oldValue.longValue() + value.longValue());
} else {
return (V) Integer.valueOf(oldValue.intValue() + value.intValue());
}
}
@Override
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
}
```
在put方法中,首先获取当前key对应的旧值,如果旧值为null,则直接将新值加入map中;否则,计算出新值,并用新值替换旧值。
需要注意的是,value需要是Number的子类才能进行加法操作。由于加法可能涉及到不同类型的Number,因此在sum方法中进行了类型判断和转换。
使用时,可以将原始的Map传入SummingMap的构造函数中,即可得到一个支持相同key的value相加的Map。例如:
```java
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("a", 3);
Map<String, Integer> summingMap = new SummingMap<>(map);
summingMap.put("a", 4);
System.out.println(summingMap); // 输出 {a=7, b=2}
```
在这个例子中,原始的map中有两个key为"a"的值,分别为1和3。将原始map传入SummingMap的构造函数中得到summingMap,然后将新值4放入summingMap中,此时summingMap中key为"a"的值应为1+3+4=7,而key为"b"的值不变。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)