How to update Object key value conditionally using spread operator? [duplicate] - javascript

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: '' })
};

Related

How can i check the property of a object, and if that object has that property how to hold that property into a variable to use it [duplicate]

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}`)
}

referencing object property generically in javascript [duplicate]

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,
},
});

How to access object data using variable value as element in vue js [duplicate]

This question already has answers here:
Access object child properties using a dot notation string [duplicate]
(13 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 months ago.
How i can access the object data using variable value as key?
Let say i have this object:
let item = {
name: {
first_name: "Mark",
last_name: "James"
}
}
How i can get the value of first name using variable value?
I know i can access it using
item.name.first_name
What if i want to access it using variable value like this:
let key = "name.first_name"
console.log(item[key])

JavaScript: Object Property Value Shorthand for nested assignment [duplicate]

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})

How to deal with JSON Objects containing Bad ID's [duplicate]

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]);
}

Categories

Resources