javascript 如何获取元素在数组中的位置 key
1个回答
展开全部
var a=new Array({key:'1',value:"a"},{key:'2',value:"b"},{key:'3',value:"c"},{key:'4',value:"d"});
下面写了个例题
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT=""></HEAD><BODY><SCRIPT LANGUAGE="JavaScript"><!--function Map( ){
this.elements = new Array();
this.init = function( _e ){
this.elements = _e ;}this.put = function(key, value) {
// 加入元素
this.elements.push({key: key, value:value});}this.getByValue = function( _value) {
// 取出对应valuetry{for (i = 0; i < this.elements.length; i++) {
if (this.elements[i].value == _value) {
return this.elements[i].key;}}}catch(e) {
return null;}}this.getByKey = function( _key) {
// 取出对应keytry{for (i = 0; i < this.elements.length; i++) {
if (this.elements[i].key == _key) {
return this.elements[i].value;}}}catch(e) {
return null;}}}//使用例题var a=new Array({key:'1',value:"a"},{key:'2',value:"b"},{key:'3',value:"c"},{key:'4',value:"d"});
var value = "b";
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询