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