This question already has answers here:
What's happening when I use for(i in object) in AS3?
(2 answers)
Closed 8 years ago.
When i try to print the object, it simply print in reverse.
Code:
var marcos:Object = new Object();
marcos.nome = "Marcos";
marcos.ano = 19;
for (var prop in marcos)
{
trace(prop + ":" + " " + marcos[prop]);
}
Output:
ano: 19
nome: Marcos
I had search in the adobe documentation about object and for each but nothing seems ot explain that.
When i try to put more elements the object simply get randomic orders, i really dont know what's going on, if someone could help me i would be grateful.
That's just the way it works with a for in loop and a non-array Object. It's documented on the Adobe website:
The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are not kept in any particular order, so properties may appear in a seemingly random order).
Related
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
Assuming I've a JSON object like this:
var myObj = {
'question1': {
'option1': 'foo',
'option2': 'bar',
'option3': 'baz'
},
'question2': {
...
},
'question3': {
...
}
};
And since its children always has a number in its keys, I want to do a loop and concatenate the loop's index to the object keys, and get the values in the dot notation method...
So, I guess to get the values, I need to do some thing like this:
myObj.'question'+i
How can I do the concatenation right?
Simply do
myObj['question'+i]
This is because the dot operator would not accept string with it as per javascript. So you will have to use square brackets instead which is often used to access properties of an object dynamically.
This question already has answers here:
Javascript appending object doesn't guarantee order
(3 answers)
Is the underscore prefix for property and method names merely a convention?
(6 answers)
Closed 3 years ago.
I'm using redux and a piece of my state has this structure:
{cityId: {name:cityName, population: numberOfPeople}}
something like:
{
1324:{name:"Clagary", population:1234, id: 1324},
46283: {name:"Edmonton", population: 5678, id: 46283}
}
Everything was fine till i needed to show them in a list with the same order that they have on the main object. At this point, I figured out that objects will auto-sort the keys if they are numbers. I tried to change them to string by putting them inside a "" but it didn't work ({"1324":{...}}).
So i tried adding and underline character as:
({"_1324":{...}})
Now it's working but I just have to double-check with some of you experts here to make sure that this is a good practice and it's normal to do it this way or if there is any better way to deal with numbers as keys in an object when the order matters.
So I'm asking react/redux experts to see if they would move to other solutions when they need the order or would just do what i did.
P.S. I really don't want to go back to use an array as this type of object store is much easier and handy for us in many ways.
This question already has answers here:
How to copy end of the Array in swift?
(6 answers)
Closed 6 years ago.
Swift's implementation of arrays is throwing me for a loop again! (Haw, haw.) All I want is to take an array of arbitrary length and get a new array (or an array slice) from it that has the first element removed.
Why is this so hard?
I just want to be able to do something like this:
let suffix = someArray.suffixFrom(1)
But I can't figure out how to do this. It seems the closest method I've found requires knowing the length of the array, which I don't know because it's computed inline and I really really hate having to split such a simple concept up into a bunch of variables and lines.
Elaboration:
I have a string split by colons (:) and I just want to create a Set containing all the :-delimited components excluding the first one.
So that a string "one:two:three" would return a set containing ["two", "three"]. Since I can create a Set from an array, all I really need is to get the suffix of the array, but I don't know how many components there are because it's inline:
return Set(attributeName?.componentsSeparatedByString(":").suffixFrom(1))
Why does Swift make this so hard?
Edit: Before a bunch of you suggest it, I'm well aware I could extend the array class to do this myself, but I'm writing a framework and I don't want to do that, and I also don't want to have to write a bloody utility function to do something so darned simple.
The CollectionType.dropFirst(_:) does exactly what you need, with the exact syntax you're looking for.
let suffix = someArray.dropFirst(1)
This question already has answers here:
Swift Object reference to array?
(4 answers)
Closed 6 years ago.
I found some really strange behavior in Swift. Here's the code:
var array2d: [[Int]] = [[1]]
print(array2d) // prints [[1]]
var first = array2d[0]
first.append(2)
print(array2d) // still prints [[1]]!!!
I would totally expect the last line to print [[1, 2]]. I can't explain the current behavior. I'd expect array2d[0] to return a reference to the first item, or possibly a copy of that reference. In either case, modifying that object should modify array2d. But that's not what's happening.
If, however, I update the array like this:
array2d[0].append(2)
it then prints [[1, 2]], as expected.
Can someone please explain this for me?
How arrays are referenced/passed around/copied in swift is a point of a lot of contention, take a look at this link.
In essence what is happening is that var first = array2d[0] is taking a copy of the array at that index as opposed to creating a reference as you were expecting. Hence accessing the array with the subscript notation allows to you to correctly alter the array but creating a new variable doesn't.
This question already has answers here:
Javascript multiple email regexp validation
(7 answers)
Closed 7 years ago.
I need to split a value from a cell into separate array values. I am taking a list of emails from my spreadsheet and need to use the addCommenters() method.
Spreadsheets cell value: email1#email.com, email2#email.com, email3#email.com ... etc.
I need to split around the ", " properly to pass the array into addCommenters().
What is the programmatic way to do this?
If you get the value of that cell (using .getValue())
var emails = sheet.getRange(range_here).getValue()
and then apply the split() method
var commentersArray = emails.split(",")
you will have an array you can pass to the .addCommenters() method.
file.addCommenters(commentersArray);