JS 数组问题,请大神来指导

qq313565187 2013-06-19 08:24:24
我从页面上拿到a,200 b,300 c,400 a,100 这样四组信息。我怎么建一个数组 放 他们,然后循环 得到新数组 a,300 b,300 c,400
...全文
162 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
天际的海浪 2013-06-20
  • 打赏
  • 举报
回复
问题没有表达清楚,弄得我们这些解答者就好像在猜谜。
wdbjsh 2013-06-20
  • 打赏
  • 举报
回复
额 错了...貌似是得用双重循环

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不支持
wdbjsh 2013-06-20
  • 打赏
  • 举报
回复
如果三个数组的内容不需要排重的话,那么循环一次就可以
不许我帅 2013-06-20
  • 打赏
  • 举报
回复
不是两层循环,内层同属性值累加,没有相同属性名新增一个。
天际的海浪 2013-06-19
  • 打赏
  • 举报
回复

<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]);
}
qq313565187 2013-06-19
  • 打赏
  • 举报
回复
比如我从页面上可以取到 信息 :甲,100。乙,200.丙,300。甲,100. 我现在要依据甲乙丙 把这些数据重新整合。变成甲200乙200丙300,然后在页面上输出甲:200.乙:200,丙:300
ddgx 2013-06-19
  • 打赏
  • 举报
回复
楼主可以考虑用下hash表 ,给你个例子参考下,hash表类网上的,修改了下;
<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>
「已注销」 2013-06-19
  • 打赏
  • 举报
回复
问题没有表达清楚

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧