This question already has an answer here:
Where can I get info on the object parameter syntax for JavaScript functions?
(1 answer)
Closed 4 years ago.
This:
x = ({ title, description }) => { console.log(title, description) }
Works like this: (at least in terms of accessing the variables)
x = (title, description) => { console.log(title, description) }
It was quite surprising to me. First the first method describes the passed parameter as an object with certain keys title and description, but what is even more surprising, one can then access the values as normal variables.
Is it some syntactic sugar? Where is it mentioned in specification? This new JS is quite confusing.
That is ES6's Destructuring assignment
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
Related
This question already has answers here:
access object through dot-syntax string path
(2 answers)
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 2 years ago.
I am having a problem which I think I might have figured out before how to do it but I can't remember now and can't figure it out.
Let's say we have an object thats a few levels deep, meaning it has as values other objects which also have as some of the values objects and so on.
Now how could I make a function to which I pass the object and and adress inside it and I can access the value at that location inside the function like this:
const getValueAtAdress = (object, 'country.city.rules') => {
return //here I need to return the value at object.country.city.rules.
}
Am I missing something obvious?
I thought I'd mention here for posterity that what helped me was the answer using the reduce which is exactly what I used before but I could not remember:
Example that I am using for my particular problem:
let stateLocation = address.split('.').reduce((acc, cur) => acc[cur], state);
Your code shows a function declaration but you can't declare an argument name in quotes
You can however call a function and pass a string.
In that case, you just need to split the string into an array and then loop over that array, building up a "chained" set of string indexes that can be passed to the object. The String.split() and Array.reduce() methods are the key.
let obj = {
county: {
city: {
rules: "Strict"
}
}
};
const getValueAtAddress = (object, countyCityRules) => {
// Split the string at the dots to form an array...
// The loop over that array and reduce it with an
// accumulator that is then applied to the object.
return countyCityRules.split(".").reduce((acc, cur) => acc[cur], obj);;
}
console.log(getValueAtAddress(obj, "county"));
console.log(getValueAtAddress(obj, "county.city"));
console.log(getValueAtAddress(obj, "county.city.rules"));
This question already has answers here:
Why were ES5 Object methods not added to Object.prototype?
(2 answers)
Why is it Object.defineProperty() rather than this.defineProperty() (for objects)?
(3 answers)
Closed 3 years ago.
There are methods like Object.values fo Object.keys, but why aren't these methods inside the object's prototype? Is there a good reason for this?
Example:
const user = { name: 'John', role: 'admin' };
const keys = user.keys() // instead of Object.keys(user);
const values = user.values() // instead of Object.values(user);
Because everything is an object in JavaScript. If you add a method to the Object's prototype, it would be inherited to everything, it can't (or shouldn't, as it then hides the original method) be used as a name of a custom method. That means that if the Object.prototype would get polluted with a lot of methods, it would make the choice of property names more difficult:
1..keys() // Did you expect this to work?
This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 6 years ago.
I was going through Map Documentation on MDN. In Examples, under Using Map Object, Object Literal - {} is used as key to store value. But, the value in Map can't be retrieved using Object Literal.
I verified this in Browser Console and found that Object Literal is not equal to itself. Also, the Function Expression - function() {} is not equal to itself.
I couldn't find the reason behind this. If required, I can ask a different question for Function Expression.
Each time you do {}, it creates a new empty object, so when you do {} == {}, you're comparing two different objects. This comparison is done by reference, so it returns false.
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I am not sure how to accurately describe this issue, but the code should suffice. This code is in React.js, but I think it is a JavaScript question more than anything. I have a variable initialization:
const key = this.state.key;
This will evaluate to one of three strings...title, author, or year.
How can I use this variable in the following block?
let filteredBooks = books.filter(
(book) => {
return book.key.toString().toLowerCase().indexOf(this.state.query) !== -1;
}
);
Book has properties title, author, and year... that I want to query, and I would rather not switch through the possible values of key(only 3 right now), in case I want to scale with more properties for the book object later. How can I dynamically reference key, and render it on the fly to either title, author, year, or any other property that key might be. (key is not a property of book). I have tried book.eval(key), but this throws an "undefined is not a function" compilation error at eval(key). Once again, I am using React.js but I am thinking this is a general Javascript question. Thanks!
book[key] is the correct answer posted by Answer Li and Keith in the comments!
This question already has answers here:
Is it possible to destructure onto an existing object? (Javascript ES6)
(16 answers)
Closed 7 years ago.
How can I write the following using new ES6 features:
this.currentPlayer = values.currentPlayer;
this.gameOver = values.gameOver;
this.inCheck = values.inCheck;
I believe I should either use destructuring operator or Object.assign function or both
AFAIK there's no way to usefully simplify that code, unless those three fields are the only ones present in values.
If (and only if) that is the case, you can just use:
Object.assign(this, values);
There is a destructuring version, but IMHO it's not worth using because it's barely any shorter than individual explicit assignments:
({currentPlayer: this.currentPlayer,
gameOver: this.gameOver,
inCheck: this.inCheck} = values);