This question already has answers here:
Can I get a javascript object property name that starts with a number?
(2 answers)
How can I access a JavaScript object which has spaces in the object's key?
(4 answers)
Closed 4 years ago.
After fetching a json object from an external API I get the following as an example:
id: 1
name: john
email: something#example.com
3a5411a124378534906a883a0c5ccda5724175eb: USA
So, in JavaScript I can easily access: object.id, object.name, etc.
However, object.3a5411a124378534906a883a0c5ccda5724175eb throws the error:
Identifier directly after number
How do deal with a situation like that? Or, in other words, how can I get the value of USA?
use
object["3a5411a124378534906a883a0c5ccda5724175eb"];
Use the for-in loop on object to access all the properties of objects as follows event the properties like you mention,
var obj = {
id: 1,
name: 'john',
email: 'something#example.com',
'3a5411a124378534906a883a0c5ccda5724175eb': 'USA'
}
for(var prop in obj){
//do the stuff here what you want for each properties
console.log(obj[prop]);
}
Related
This question already has answers here:
How do I check if an object has a specific property in JavaScript?
(31 answers)
Closed 8 days ago.
so if for example i have a object called Persona and it has the followed property
var person = { name: 'Derwyn', age: '40', occupation: student };
i would like to check all those properties in that object and if the object has a name (in this case it has) i will take that name (Derwyn) and use it in a string to form the sentences
hello my name is Derwyn, im 40 years old and im currently a student.
i tried a for in and a "hasOwnProperty" but after checking it only shows true or false, i need to hold the property in a variable
if (person.name) {
console.log(`Hello my name is ${person.name}`)
}
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed last month.
im using javascript to update a an object but I have to specify the property each time:
const user = await prisma.user.update({
where: { id: theid },
data: { email: statvalue },
})
so I want to be able to update any property, in this example only 'email' can be changed.
How would I use a string to generically replace where it says 'email' and put in a variable so i can use this short code snippet to update any property of the object?
EDIT- I can access the actual variables in an object this way, as in the suggested answers. But I cant do what I said above.
Which is getting the property itself generically and inserting it the above code instead of email.
You should be able to accomplish this like this:
const propertyToUpdate = "email";
const user = await prisma.user.update(
{
where:
{
id: theid,
},
data:
{
[ propertyToUpdate ]: statvalue,
},
});
This question already has answers here:
How to get a subset of a javascript object's properties
(36 answers)
One-liner to take some properties from object in ES 6
(12 answers)
Closed 3 years ago.
Lets say I have an object
obj = {
name: 'Karthi',
age: 25,
gender: 'Male'
}
and I want to assign some of the values to a key with the same name in another object.
objTar = {
name: obj.name,
age: obj.age
}
Is there a shorthand for the above "nested" assignment? I know there's a shorthand for flat assignments; eg., objTar = { name, age } given name and age are available in the lexical scope.
Don't suggest workarounds to achieve this please. Just let me know if this is "natively" supported.
You can achieve this using below code.
objTar = Object.assign({},{age:obj.age, gender:obj.gender})
This question already has answers here:
Self-references in object literals / initializers
(30 answers)
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 5 years ago.
I have this code
var array, key, dc;
$.post("/mailchimp/check_mailchimp_key",
{
store_id: document.getElementsByName('data[store_id]')[0].value,
mailchimp_api_key: document.getElementsByName('data[mailchimp_api_key]')[0].value,
array: mailchimp_api_key.split('-'),
key: array[0],
dc: array[1]
}
store_id and mailchimp_api_key work, but I have problem with others. This way it says mailchimp_api_key is not defined and my goal is to take whatever is stored in mailchimp_api_key and divide it to key and dc.
mailchimp_api_key: document.getElementsByName('data[mailchimp_api_key]')[0].value
… means that when the object has finished being constructed it will have a property called mailchimp_api_key with that value.
mailchimp_api_key.split('-'),
… tries to read a variable called mailchimp_api_key.
There are two problems with this:
A variable is not an object property
The object property doesn't exist yet
Copy the value to a variable before you construct the object.
Use it twice.
var array, key, dc;
var mailchimp_api_key = document.getElementsByName('data[mailchimp_api_key]')[0].value;
$.post("/mailchimp/check_mailchimp_key",
{
store_id: document.getElementsByName('data[store_id]')[0].value,
mailchimp_api_key: mailchimp_api_key,
array: mailchimp_api_key.split('-'),
key: array[0],
dc: array[1]
}
This is not Javascript at all! You can't reference Object keys that you just defined.
var mailchimp_api_key = document.getElementsByName('data[mailchimp_api_key]')[0].value;
var array = mailchimp_api_key.split('-');
var key = array[0];
var dc = array[1];
This way you get all the variables you need, then you might want to pass them in your Ajax call.
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
How to access an object using a variable as key. Here is my code sample:
var o = {"k1": "111", "k2": "222"};
alert(o.k1); //working fine
var key = "k"+1; alert(key); // k1
alert(o.key); //not working
You can access objects like arrays:
alert(o[key]);
Change the last line to: alert(o['k1']); or alert(o[key]); where key is your dynamically constructed property key.
Remember you can access object's properties with array notation.
Consider using a for...in loop