ConorReidd ConorReidd - 7 months ago 27
Javascript Question

Trying to push an array from an .each() into another array

I'm trying to push an associative array into an empty object and it's giving me the error:

Cannot read property '#<Object>' of undefined


The code I'm trying is

var arr = {};
$(".proj").each(function() {
arr.push[{name: "name"}];
});
console.log(arr);


I think it's something to do with the scope of
arr
but i'm unsure, any help is appreciated

Answer

Check this code snippet

$(document).ready(function(){
  var arr = [];
$(".proj").each(function() {
 arr.push({name: "name"})
});
console.log(arr);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="proj">
  hello
</div>

Hope this helps