87,910
社区成员
发帖
与我相关
我的任务
分享
var arr=[{key:"a",val:[1,2,3...200]},{key:"b",val:[1,2,3....100]},{key:"c",val:[1,2,200]},{key:"a",val:[1,2,..100]}];
for(var i=1;i<arr.length;i++){
var biaoj=false;
for(var j=0;j<i;j++){
if(arr[j].key==arr[i].key){
arr[j].val==arr[j].concat(arr[i].val);
arr.splice(i,1);
}
}
}
大概这个意思吧...有点不好想,另外 splice貌似低级别ie不支持
<script type="text/javascript">
var suy = "a:200,b:300,c:400,a:100";
var arr = suy.split(",");
var obj = {};
for(var i=0, l = arr.length; i < l; ++i)
{
var h = arr[i].split(":");
obj[h[0]] = (obj[h[0]]||0) + h[1]*1;
}
for (var i in obj)
{
document.write(i,":",obj[i]," ");
}
</script>
function Objs(j, y, b) {
this['甲'] = j;
this['乙'] = y;
this['丙'] = b;
}
var objs1 = new Objs(100, 200, 300);
var objs2 = new Objs(200, 200, 300);
for(var i in objs2) {
alert(i + ':' + objs2[i]);
}
<script type="text/javascript">
function Hashtable() {
this._hashValue= new Object();
this._iCount= 0;
}
Hashtable.prototype.add = function(strKey, value) {
if(typeof(strKey) == "string"){
if(this.contain(strKey)){//这段修改过,为了满足你的需求
this._hashValue[strKey]= typeof(value) != "undefined"? value+this._hashValue[strKey] : null;
}else
{
this._hashValue[strKey]= typeof(value) != "undefined"? value: null;
this._iCount++;
}
return true;
}
else
throw "hash key not allow null!";
}
Hashtable.prototype.get = function(key) {
if (typeof(key)== "string" && this._hashValue[key] != typeof('undefined')) {
return this._hashValue[key];
}
if(typeof(key) == "number")
return this._getCellByIndex(key);
else
throw "hash value not allow null!";
return null;
}
Hashtable.prototype.contain = function(key) {
return this.get(key) != null;
}
Hashtable.prototype.findKey = function(iIndex) {
if(typeof(iIndex) == "number")
return this._getCellByIndex(iIndex, false);
else
throw "find key parameter must be a number!";
}
Hashtable.prototype.count = function () {
return this._iCount;
}
Hashtable.prototype._getCellByIndex = function(iIndex, bIsGetValue) {
var i = 0;
if(bIsGetValue == null) bIsGetValue = true;
for(var key in this._hashValue) {
if(i == iIndex) {
return bIsGetValue ? this._hashValue[key] : key;
}
i++;
}
return null;
}
Hashtable.prototype.remove = function(key) {
for(var strKey in this._hashValue) {
if(key == strKey)
{
deletethis._hashValue[key];
this._iCount--;
}
}
}
Hashtable.prototype.clear = function () {
for (var key in this._hashValue) {
delete this._hashValue[key];
}
this._iCount = 0;
}
</script>
<script type="text/javascript">
var hash=new Hashtable();//简单写了下
hash.add("a",1);
hash.add("b",3);
hash.add("c",13);
hash.add("c",6);
hash.add("a",7);
var v=[];
for(var i=0;i<hash.count();i++){
v.push(hash.findKey(i)+","+hash._getCellByIndex(i));
}
alert(v.join(" "));
</script>