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.
Related
This question already has answers here:
Check if every element in one array is in a second array
(10 answers)
Closed 2 years ago.
I am trying to filter my entites by tags array, that can look like this:
const tags = ["tag1", "tag2"];
Every entity has property tags, that can have existing tags, for example:
["tag1", "tag2", "tag3"];
Or:
["tag1", "tag2"];
I need to compare if the tags array and the tags array of entity has the same values.
So far I have tried this code, but it returns an entity even if the two arrays dont have the same values (I'm guessing the includes() function is to blame).
tags.every((tag: any) => doc.tags.includes(tag));
Any ideas how can I check if the arrays have the same values?
Thank you in advance.
You can also compare the length as well
tags.every((tag: any) => doc.tags.includes(tag)) && tags.length === doc.tags.length;
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:
Nodejs: url.parse issue with arrays inside query
(2 answers)
Closed 6 years ago.
I'm working in a MEAN app, in which I want to pass an array value to url and fetch values from mongodb. Actually it's a lat and lng. My url should look like:
http://localhost:8080/search?type=latlng&value=[0.123, 0.456]
And have to fetch data related to that lat lng. How to do that?
You cannot pass literal array objects in the URL like you propose. There are workarounds however. You must either separate the data into {key}={value} pairs within the URL, or use a POST request.
GET: http://localhost:8080/search?type=latlng&lat=0.123&lng=0.456
http://localhost:8080/search?type=latlng&coords[]=0.123&coords[]=0.456
POST: http://localhost:8080/search
data: { type: 'latlng', value: [0.123, 0.456] }
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);
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).