Given
var input[[]];
$('some_selector').each(function() {
var outer, inner;
outer=$(this).parent().attr('some_property');
inner=$(this).attr('a_property');
input[outer].push(inner);
});
An error is encountered during the push
function. Is it because the specific input[outer]
is not declared as array?
Also, the value of outer
is not necessarily sorted. So within the loop, outer
can sequentially have values of: "property1","property2","property1","property3","property2" ...
In PHP terms, is there something equivalent to:
foreach () {
$input[$outer][]=$inner;
}
Thanks!
If outer
has values like "property1", etc., then input
isn't an array. It's an object. Unlike PHP, there are no associative arrays in Javascript.
Try:
var input = {};
And yes, you'll need to create an array before pushing to it. You can do that in one "maybe it exists, maybe it doesn't" step like so:
input[outer] = input[outer] || [];
and then push as before:
input[outer].push(inner);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.