How to call object property in another key before execution [duplicate] - javascript

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 3 years ago.
I want to call name property in another key. Is it possible? If yes, then how can we achieve this?
let user = {
name: "John",
age: this.name,
};
console.warn(user)

Do you mean like this?
What is it you are trying to achieve?
let userA = {
name: "John",
age: 8
};
let user = {
name: "John",
age: userA.name
};
console.log(user)

I am not in a position to test it at the moment, but I think, in theory, this should work:
let user = {
name: "John"
};
user.age = user.name
console.log(user.age);
// output: John

Related

How to declare object with different name in Javascript? [duplicate]

This question already has an answer here:
ES6/ES2015 object destructuring and changing target variable
(1 answer)
Closed 2 years ago.
Let's say i declaring an object
let Mahasiswa = {
name: "Steve",
age: 22
}
And then later in my code i copy these code with same name;
let {name, age} = Mahasiswa;
console.log(name, age) // Steve 22
but how do i change it to different variable name?
let {name as Player, age as ID} = Mahasiswa;
console.log(Player, ID) // Steve 22
We use :
let Mahasiswa = {
name: "Steve",
age: 22
}
let {
name: Player,
age: ID
} = Mahasiswa;
console.log({ Player, ID });

Destructure object and assign to another object in one line [duplicate]

This question already has answers here:
Is it possible to destructure onto an existing object? (Javascript ES6)
(16 answers)
Closed 2 years ago.
Is there a way to do the following in one line?
let person = {}
const { firstName, lastName } = getNames()
//or this
//const { firstName, lastName } = await getNames()
person.firstName = firstName
person.lastName = lastName
I often do this when coding, and hoping there is a shortcut. I can not see any hints on how to do this on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment.
I was trying something like the below however, it overrides the other properties in the object.
let person = { age: 20 }
person = { ...getNames() }
I don't think this will work well with async/await functions either, as they return a promise.
let person = { age: 20 }
person = { ...await getNames() }
You could probably try something like this:
({firstName: person.fistName, lastName: person.lastName} = getNames());
You would need person defined as an object beforehand.
You can use Object.assign for this.. For example.
let person = { firstName: 'John', lastName: 'Doe', age: 67, //etc... }
let newPerson = Object.assign(person, getNames())
console.log(newPerson)
// Expected output: `{ firstName: 'newFirstName', lastName: 'newLastName', age: 67, etc... }`
You can view more on Object.assign here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Remove property from a copied object while retaining property in original object [duplicate]

This question already has answers here:
How do I correctly clone a JavaScript object?
(81 answers)
Closed 3 years ago.
Why is my Employee object being changed when I've replicated it and I'm making changes to my new variable?
const Employee = { firstname: 'John', lastname: 'Doe' }
const EmployeeModifier = Employee;
console.log(EmployeeModifier.firstname);
delete EmployeeModifier.firstname;
console.log(Employee.firstname);
Right now this returns
> "John"
> undefined
Ideally it would return
> "John"
> "John"
But something is causing the delete command to remove from BOTH Employee and EmployeeModifier. Why? And how can I change this?
Because it's a reference to the original object. You will need to do a shallow/deep copy.
This is probably sufficient for you:
const oldObject = {
firstname: 'John',
address: {
street: '123 Street',
city: 'Fake Town'
}
}
const newObject = JSON.parse(JSON.stringify(oldObject));
This method has it's own problems, though. You can also use lodash's cloneDeep which is going to cover more cases.
Read here for more information on shallow/deep copying.

What does this variable declaration form mean? [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 3 years ago.
It is that situation, when you get some code, which works, but you don't know how.
What does this declaration method do?
const { actions: { createRole, updateRole } = {} } = props;
The code uses destructing for an nested object. The following example might help for understanding this new JavaScript syntax (has been introduced with ES6):
const user = {
id: 339,
name: 'Fred',
age: 42,
education: {
degree: 'Masters'
}
};
const {education: {degree}} = user;
console.log(degree); //prints: Masters
I would recommend the following resource for further examples:
https://medium.com/#pyrolistical/destructuring-nested-objects-9dabdd01a3b8

Javascript spread operator [duplicate]

This question already has answers here:
Object destructuring solution for long arrays?
(2 answers)
Closed 4 years ago.
I want to get these properties from an object using es6 directly on the parameters list of the function but I don't know how to do it exactly:
function methodA(person){
var driverName = person.name,
age = person.age,
company = person.job.company;
...
}
Any tips in that direction?
Take a destructuring assignment.
function methodA(person) {
var { name: driverName, age, job: { company } } = person;
console.log(driverName, age, company);
}
methodA({ name: 'Grace', age: 49, job: { company: 'Infinity' } })

Categories

Resources