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,
},
});
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:
In JavaScript, how to conditionally add a member to an object?
(29 answers)
Conditionally set an object property
(7 answers)
How to conditionally add properties to a javascript object literal
(8 answers)
Closed 8 months ago.
I need to update Existing object conditionally using spread operator.
Below my code
const resetPerson: any = {...person, name: '', age: '', location && location : '' };
if location key exist in person object then only clear location else no need to add location property.
How can I do that.
You can conditionally add an object property like this:
const resetPerson: any = {
...person,
name: '',
age: '',
...('location' in person && { location: '' })
};
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
I currently have the following in my Javascript code where Id is a field of an object:
this.recId = event.detail.row.Id;
I want to make the Id part generic something like
String a = 'Id';
this.recId = event.detail.row.a;
How do I achieve this in Javascipt
Use [ ] instead.
const obj = {
id: "myObject"
}
const a = "id"
console.log(obj[a]);
In your example, it would be
this.recId = event.detail.row[a];
You can use
this.recId = event.detail.row[a] to get the desired result
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]);
}
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
I'm using Chrome's version of localStorage (chrome.storage.sync) and I want to use a variable key and value when setting what data to store with it.
I'd like to be able to do something like this:
chrome.storage.sync.set({ user: id }, callback);
where the user and id are dynamically generated.
This:
var user = "Bob";
var id = "81256309";
chrome.storage.sync.set({ user: id }, callback);
doesn't work because it doesn't interpret user and id as their underlying string values and, as a result, syncs to Chrome with this object: { user: id }.
I know how to do this with normal JavaScript objects but the method in that thread won't work in this case because I don't have control over the storage.sync object of my Google account.
See Computed property names in ES6, you could use
chrome.storage.sync.set({ [user]: id }, callback);