admin管理员组文章数量:1296448
this is what I've got and been struggeling for hours. if I alert(i)
in the each loop it gives me 1,2,3...
but if I want to use as as key for a multidimensional array it is like a string "i"
$(document).ready(function(){
var positions=[];
$( ".box" ).each(function(i) {
//alert(i);
var elPositions = {};
elPositions.i = $(this).offset().top;
positions.push(elPositions);
//$elPosArray[i] = $(this).offset().top;
//$(this).html('outer height--> ' + $(this).outerHeight(true));
});
console.log(positions);
//console.log(el);
});
There are Questions and answers to this topic but none of them helped me to get this to work.
I would like to get an array or obj looking something like:
positions[0]['offset'] = '120';
positions[0]['height'] = '300';
positions[1]['offset'] = '420';
positions[1]['height'] = '180';
positions[2]['offset'] = '600';
positions[2]['height'] = '100';
positions[3]['offset'] = '700';
positions[3]['height'] = '300';
Here is a fiddle with the html /
this is what I've got and been struggeling for hours. if I alert(i)
in the each loop it gives me 1,2,3...
but if I want to use as as key for a multidimensional array it is like a string "i"
$(document).ready(function(){
var positions=[];
$( ".box" ).each(function(i) {
//alert(i);
var elPositions = {};
elPositions.i = $(this).offset().top;
positions.push(elPositions);
//$elPosArray[i] = $(this).offset().top;
//$(this).html('outer height--> ' + $(this).outerHeight(true));
});
console.log(positions);
//console.log(el);
});
There are Questions and answers to this topic but none of them helped me to get this to work.
I would like to get an array or obj looking something like:
positions[0]['offset'] = '120';
positions[0]['height'] = '300';
positions[1]['offset'] = '420';
positions[1]['height'] = '180';
positions[2]['offset'] = '600';
positions[2]['height'] = '100';
positions[3]['offset'] = '700';
positions[3]['height'] = '300';
Here is a fiddle with the html http://jsfiddle/Z9WrG/
Share Improve this question asked Jan 23, 2014 at 14:38 carambacaramba 22.5k20 gold badges93 silver badges133 bronze badges1 Answer
Reset to default 7You're pretty much there!
In your loop, elPositions
(here renamed data
) is recreated new on each iteration, and then pushed into the array with a consecutive index. There's no need to specify i
in the data object as i
is assigned automatically when you push into the array.
See updated fiddle: http://jsfiddle/Z9WrG/2/
and code:
$(document).ready(function(){
var positions=[];
$( ".box" ).each(function() {
var $this = $(this);
var data = {};
data.offset = $this.offset().top;
data.height = $this.height();
positions.push(data);
// Now, positions[iteration_index] = { offset: x, height: y }
});
console.log(positions);
console.log(positions[0].height);
console.log(positions[0].offset);
});
本文标签: javascriptcreate multidimensional array or object in jquery each loopStack Overflow
版权声明:本文标题:javascript - create multidimensional array or object in jquery each loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741637440a2389721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论