Full Object, but empty array. $.each not hitting - backbone.js

![information is clearly there, but not being found][1]
Does anyone have any idea what I can do to my code to make this $.each / _.each work? (I'm using both jquery and underscore, either will do)
The Array[0] concerns me. Maybe that is why the .each is getting stepped over.
The two console.log's on either side of the each log the entire object perfectly, but when I break at the each and type "regionedAps" into the console, I get an empty array.

You have an array that you are treating like an object. You should make regionedAps an object, then .each will iterate over it properly.
var regionedAps = {};
I don't think you need $.makeArray()

Related

How to map array of objects

I have some problems mapping an array of objects in JS.
This is how the response looks in my console:
[{...}]
...and when I expand it I get this:
0:{id:0, document:{...}}
1:{id:1, document:{...}}
Usually the response I get is always without this number in front of each object, like this:
{id:0, document:{...}
{id:1, document:{...}
I tried every approach I know and I cant't manage to handle it.
The goal is to take each value out of "document" property and dynamically display it in some kind of table.
This is the way the browser devtools decides to display the array, but it is actually correct, if I'm not mistaken you're still dealing with an array.
You can verify this by logging the following:
(Replace myVar with the variable name you chose for your response array)
console.log(Array.isArray(myVar))
If it outputs true then you're fine and you are dealing with an array.

Why was I able to get away with not using a splat operator sometimes

I have some ruby code from my Ruby on Rails project.
I am formatting some data so I am calling attributes.extract! to get the fields I need from my model.
I noticed recently that every now and then the data wouldn't get extracted as expected. I realized that I needed a splat operator. But it is strange because I do notice that sometimes when the method is called in my Rails project it will sometimes extract the data without the use of the splat operator. But when I run the code from my Rails console it never extracts the data unless I add the splat operator.
Here is the code in question
# in a service file, let's call it service.rb
def self.format_user_home_address_data(user)
# This doesn't work in the console but sometimes works when run in my Rails project
home_address_data = user.attributes.extract!(User::HOME_ADDRESS_FIELDS)
home_address_data[:address_type] = "home"
home_address_data
end
# at the end this method will sometimes return { address_type: "home" } or
# sometimes it'll actually return the extracted attributes as expected
HOME_ADDRESS_FIELDS is just an array with the values ["address_line_1", "city", "state", "zip"]
Anyway I know that to get it to run correctly I need to do this
home_address_data = user.attributes.extract!(*User::HOME_ADDRESS_FIELDS)
But does anyone know why I was able to get away without adding the splat operator for so long? Is there some Ruby on Rails magic that is only sometimes happening? What's the deal?
Well, let's check it out. There is no any magic behind attributes.extract! in the end. Here is an actual implementation of this method from Rails source code:
def extract!(*keys)
keys.each_with_object(self.class.new) { |key, result|
result[key] = delete(key) if has_key?(key)
}
end
Link: click. As you can see, it creates new hash, goes over the keys one by one and moves value from self to this new array. So, if you give an Array argument to this method then key in the block will be an Array as well. So, it won't be found. So, no way it may work for array argument. The only one possibility is that something else is passed instead of User::HOME_ADDRESS_FIELDS.

Angular copy not working on copying array

It seems that angular.copy() is not properly working on one of the items that I am using it on. Here's the sample code and the screenshot that follows.
console.log("Copy");
$scope.traffic_data = traffic_data;
$scope.total_data = total_data;
console.log($scope.traffic_data);
console.log($scope.total_data);
console.log("Original");
$rootScope.original_traffic_data = angular.copy($scope.traffic_data);
$rootScope.original_total_data = angular.copy($scope.total_data);
console.log($rootScope.original_traffic_data);
console.log($rootScope.original_total_data);
console.log("Variable data");
console.log(total_data);
console.log("=============");
The problem I am facing is that the
$rootscope.original_total_data
is not copying the contents of the
$scope.total_data
as seen on the screenshot. I have highlighted the different console logs to differentiate them from one another.
The line
console.log($rootScope.original_total_data);
shows no contents even though I have used angular.copy on that variable.
What am I missing here? Please help. Thanks.
Also $rootScope is already declared in the controller and it is working for the
$rootScope.original_traffic_data
so why is it not working for
$rootScope.original_total_data?
Thanks.
total_data is an array, whereas traffic_data is an object.
angular.copy() distinguishes between arrays and objects. For objects it will copy all the keys (properties). For arrays, it will only copy the array elements and not any custom properties attached to it - see source code.
If you want to set properties on total_data, you should make it into an object instead. It does not appear to have any indexed values, so this should not be a problem, and it probably should have been an object in the first place.

Array (class) filled with non nil values stays empty

I am currently having trouble filling up an array of customClass.
I try to fill it with a jsonFile. During my json parsing (using swiftyJSON) i loop and fill my array.
The problem is, at the end of my loop, it is still empty. I tested it in different ways, and here is my code:
That's the file where the problem is. In my loop I fill an Annotation, that I add with append to my array. The problem is what my print return. Here is a part of it:
It's just a small part of a huge jsonfile. And, my tmpAnnot.name is correctly printed every iteration. But when it comes to my Array, nothing.
So I'm completly lost and hope you could help me ^^
(And for the information, here is my custom class) :
And btw, I tried to print my array.count, and it's nil too
Im so sorry if the question has been posted. I couldn't find it in the entire website.
Change your JSONAnnotationList declaration to be an non-optional and assign it an empty array
var JSONAnnotationList: [UGOAnnotation] = []
You see, you have never created an array so there was nothing to be printed.
The whole point of optionals is to use them sparingly, not everywhere.

Array won't refresh itself after removeChild

There is an array of objects. I'm trying to removeChild an object from that array like below. removeChild works fine but the array won't refresh itself after removing uppest object. As you can see in below, i tried to trace array items out.
Firstly, array has three items, obviously the myArray.length must be 3.
After removing a child, myArray.length must be 2, but it get 3 (Wrong).
removeChild(myArray[currShape]);
trace(myArray);
Please tell me what am i missing here.
Assuming you're using ActionScript, removeChild() only serves to take objects off the stage. It doesn't take things out of an array. You have to take the object out of the array manually in another statement.
You could try something like:
removeChild(myArray.splice(currShape,1));
This removes the entry from the array and returns that entry that will be used to remove it from the stage.

Resources