首先js暂时还没有map类型。所以需要先自己定义一个Map类型。代码如下
function Map () {/**
* 结构
* @param key
* @param value
*/
function Struct(key,value) {
this.key = key
this.value = value
}
/**
* 数据存放数组
*/
this.arr = []
/**
* 增加数据
* @param key {String}
* @param value {Object}
*/
this.put = function (key,value) {
for (var i = 0 i < this.arr.length i++) {
if (this.arr[i].key === key) {
this.arr[i].value = value
return
}
}
this.arr[this.arr.length] = new Struct(key,value)
}
/**
* 通过key获取数据
* @param key {String}
* @returns {Object}
*/
this.get = function (key) {
for (var i = 0 i < this.arr.length i++) {
if (this.arr[i].key === key) {
return this.arr[i].value
}
}
return null
}
/**
* 删除数据
* @param key{String}
*/
this.remove = function (key) {
var v
for (var i = 0 i < this.arr.length i++) {
v = this.arr[i]
if (v.key === key) {
this.arr.splice(i,1)
return
}
}
}
/**
* 是否存在key
* @param key {String}
* @returns {boolean}
*/
this.containsKey = function (key) {
var v
for (var i = 0 i < this.arr.length i++) {
v = this.arr[i]
if (v.key === key) {
return true
}
}
return false
}
/**
* 获取map数据量
* @returns {Number}
*/
this.size = function () {
return this.arr.length
}
/**
* 判断Map是否为空
* @returns {boolean}
*/
this.isEmpty = function () {
return this.arr.length <= 0
}
/**
* 全部清空
*/
this.removeAll = function () {
this.arr = []
}
}
然后放入数据再做判断
var map = new Map()map.put("str",'aaaaaa')
console.log(map.get('str'))
//判断是否为Map类型。 instanceof
console.log(map instanceof Map)
1.将json对象转化为json字符串,再判断该字符串是否为"{}"
var data={}
var b=(JSON.stringify(data) =='{}')
if(b==true){
console.log('是的')
}else{
console.log('不是')
}
2.for in 循环判断
var obj = {}
var b = function() {
for(var key in obj) {
return false
}
return true
}
alert(b())//true
4.Object.getOwnPropertyNames()方法
此方法是使用Object对象的getOwnPropertyNames方法,获取到对象中的属性名,存到一个数组中,返回数组对象,我们可以通过判断数组的length来判断此对象是否为空
注意:此方法不兼容ie8,其余浏览器没有测试
var data = {}
var arr = Object.getOwnPropertyNames(data)
alert(arr.length == 0)//true
5.使用ES6的Object.keys()方法
与4方法类似,是ES6的新方法, 返回值也是对象中属性名组成的数组
var data = {}
var arr = Object.keys(data)
alert(arr.length == 0)//true
6.Object.keys(res.data.list).map(id =>{
let bankList = {
code: id,
text: res.data.list[id]
}
this.columns.push(bankList)
})
给个实例,代码如下:Array.prototype.remove = function(s) {
for (var i = 0i <this.lengthi++) {
if (s == this[i])
this.splice(i, 1)
}
}
/**
* Simple Map
*
*
* var m = new Map()
* m.put('key','value')
* ...
* var s = ""
* m.each(function(key,value,index){
* s += index+":"+ key+"="+value+"/n"
* })
* alert(s)
*
* @author dewitt
* @date 2008-05-24
*/
function Map() {
/** 存放键的数组(遍历用到) */
this.keys = new Array()
/** 存放数据 */
this.data = new Object()
/**
* 放入一个键值对
* @param {String} key
* @param {Object} value
*/
this.put = function(key, value) {
if(this.data[key] == null){
this.keys.push(key)
}
this.data[key] = value
}
/**
* 获取某键对应的值
* @param {String} key
* @return {Object} value
*/
this.get = function(key) {
return this.data[key]
}
/**
* 删除一个键值对
* @param {String} key
*/
this.remove = function(key) {
this.keys.remove(key)
this.data[key] = null
}
/**
* 遍历Map,执行处理函数
*
* @param {Function} 回调函数 function(key,value,index){..}
*/
this.each = function(fn){
if(typeof fn != 'function'){
return
}
var len = this.keys.length
for(var i=0i<leni++){
var k = this.keys[i]
fn(k,this.data[k],i)
}
}
/**
* 获取键值数组(类似Java的entrySet())
* @return 键值对象{key,value}的数组
*/
this.entrys = function() {
var len = this.keys.length
var entrys = new Array(len)
for (var i = 0i <leni++) {
entrys[i] = {
key : this.keys[i],
value : this.data[i]
}
}
return entrys
}
/**
* 判断Map是否为空
*/
this.isEmpty = function() {
return this.keys.length == 0
}
/**
* 获取键值对数量
*/
this.size = function(){
return this.keys.length
}
/**
* 重写toString
*/
this.toString = function(){
var s = "{"
for(var i=0i<this.keys.lengthi++,s+=','){
var k = this.keys[i]
s += k+"="+this.data[k]
}
s+="}"
return s
}
}
function testMap(){
var m = new Map()
m.put('key1','Comtop')
m.put('key2','南方电网')
m.put('key3','景新花园')
alert("init:"+m)
m.put('key1','康拓普')
alert("set key1:"+m)
m.remove("key2")
alert("remove key2: "+m)
var s =""
m.each(function(key,value,index){
s += index+":"+ key+"="+value+"/n"
})
alert(s)
}
html代码:
[c-sharp] view plain copy
<html>
<head>
<title>Test Map</title>
<mce:script language="javascript" src="map.js" mce_src="map.js"><!--
// --></mce:script>
</head>
<body>
<input type="button" value="test" onclick="testMap()">
</body>
</html>