What does this variable declaration form mean? [duplicate] - javascript

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

Related

What's wrong with this method? the .push() method isn't working as expected [duplicate]

This question already has answers here:
Array.push return pushed value?
(7 answers)
Closed 4 months ago.
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject(subject) {
this.subjects = this.subjects.push(subject); //what's wrong with this line
}
}
student1.addSubject('Math');
console.log(student1.subjects);
// logs out 1 instead of ['Math'], .push isn't functioning properly
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject: function(subject) {
this.subjects.push(subject);
}
}
student1.addSubject('Math');
console.log(student1.subjects);
Array.push() returns the new length of the array, not the array itself.
Unless you have a reason to capture this value, you don't need to assign it:
addSubject(subject) {
this.subjects.push(subject);
}

how does usage of braces change the value in javascript? [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
Javascript object literal: what exactly is {a, b, c}?
(3 answers)
Closed last year.
I have the below Javascript code:
const product = await productsRepo.getOne(req.params.id);
console.log(product);
console.log({product});
Output from first console.log.
{ title: 'hey12up', price: 1234, image: '', id: 'b6ff5da7' }
Output from second console.log:
{
product: { title: 'hey12up', price: 1234, image: '', id: 'b6ff5da7' }
}
Though I understand that in the second output, product is a key and the value is an object (which are further key-value pairs), I dont understand how this conversion is done?
Just by enclosing the object name "product" inside braces, what is happening? Is there any technical term for this?

Using dot notation with variable to get object value in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I have a config object like this
{ config: {
params: {
football: {
url: ''
},
soccer: {
url: ''
}
}
}
I simply need to get at the football or soccer url value using a variable, so something like
let sport = 'soccer';
let path = config.params.`sport`.url;
I've tried bracket notation like config.params[sport] and the eval function but neither seem to do the trick. I'm sure I'm misnaming what I'm trying to do which is likely why I can't seem to find an answer.
thanks
This should give you an idea.
const sport = 'soccer'
const data = {
config: {
params: {
football: {
url: '1'
},
soccer: {
url: '2'
}
}
}
}
console.log(data.config.params[sport].url)

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

Accessing some data of an object inside an array of objects [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
Been looking around haven't find a solution.. Might be a really stupid question (probably) but haven't found a way to access to it.
I have the class Iphone:
export class Iphone{
version: string;
fixes = [
{fixlcdprice:null},
{fixspeakerprice:null}
];
}
then I have an array of Iphone with data
export const IPHONES:Iphone[]=[
{
version:'Iphone 4',
fixes:[
{fixlcdprice:19},
{fixspeakerprice:19}
]
},
{
version:'Iphone 4s',
fixes: [
{fixlcdprice:19},
{fixspeakerprice:29}
]
}
]
trying to access the price of the fixes but I can't.
have tryed
Iphone.fixes[0] <-- returns (object, object)
then tryed
Iphone.fixes[0[0]] <-- returns nothing..
Iphone.fixes.fixlcdprice <-- doesnt work
Looks like you want
Iphone.fixes[0].fixlcdprice
Iphone=[
{
version:'Iphone 4',
fixes:[
{fixlcdprice:19},
{fixspeakerprice:19}
]
},
{
version:'Iphone 4s',
fixes: [
{fixlcdprice:19},
{fixspeakerprice:29}
]
}
]
Iphone[0].fixes[0] gives you the disired result

Categories

Resources