All Questions
Tagged with object ecmascript-6
1,091
questions
167
votes
2
answers
101k
views
Destructuring and rename property
const a = {
b: {
c: 'Hi!'
}
};
const { b: { c } } = a;
Is it possible rename b in this case? I want get c and also rename b.
119
votes
4
answers
67k
views
Succinct/concise syntax for 'optional' object keys in ES6/ES7? [duplicate]
There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript:
const obj = {
requiredKey1: ...,
requiredKey2: ...
}...
118
votes
1
answer
62k
views
Destructuring nullable objects
Typescript (or should we say ES) doesn't allow destructuring of null/undefined objects. It throws TypeError.
So, lets say we have something like
let {a,b,c} = D;
where D could be null.
If we need ...
86
votes
10
answers
121k
views
How to create a derived class in JavaScript?
I have a base class:
function Monster() {
this.health = 100;
}
Monster.prototype.growl = function() {
console.log("Grr!");
}
That I want to extend and create another class with:
function ...
46
votes
8
answers
53k
views
jest .each name access object key
Is it possible to access an object's key inside the name portion of a .each?
let accounts =
[
{
details:
{
company_name:
"...
39
votes
5
answers
35k
views
What is a good way to automatically bind JS class methods?
I get tired of writing code like this:
class Something {
constructor() {
this.method = this.method.bind(this);
this.anotherOne = this.anotherOne.bind(this);
// ...
}
...
39
votes
1
answer
19k
views
Can I use continue and break in Javascript for...in and for...of loops?
Can I use the break and continue statements inside the for...in and for...of type of loops? Or are they only accessible inside regular for loops.
Example:
myObject = {
propA: 'foo',
propB: '...
38
votes
5
answers
13k
views
Difference between Configurable and Writable attributes of an Object
I saw the following regarding javascript, object data property attributes
—
Configurable: Specifies whether the property can be deleted or changed.
— Enumerable: Specifies whether the property can ...
35
votes
12
answers
106k
views
Select a property from an array of objects based on a value : Javascript
I have an array of objects with the following structure:
var arr = [
{
"value": "abc",
"checked": true
},
{
"value": "xyz",
"checked": false
},
{
"value": "...
35
votes
6
answers
30k
views
Object.keys using numbers in typescript
type Foo = { [key: number]: string }
const foo: Foo = { 100: 'foo', 200: 'bar' }
const sizes: number[] = Object.keys(foo)
Gives me:
Type 'string[]' is not assignable to type 'number[]
Why does ...
29
votes
2
answers
5k
views
What is an "internal slot" of an object in JavaScript?
I've tried to understand ECMAScript 2015 specification in one point: Internal Slots of Objects. But this section appeared very unclear to me, especially this sentence:
Internal slots correspond to ...
24
votes
9
answers
51k
views
How to filter an object by its values in ES6
What is the best way to filter an object this way in ES6?
Starting data:
const acceptedValues = ["value1", "value3"]
const myObject = {
prop1: "value1",
prop2: &...
21
votes
5
answers
10k
views
ES6 Classes - Updating Static Properties
I am trying to figure out alternative ways to set a static (or class) property an ES6 Class and then change it after new instances of the class are created.
For example, lets say I have a class ...
19
votes
11
answers
431
views
How to rename object keys inside array
I have an array of objects like this:
const customers = [
{
customer_name: 'Negan',
customer_age: 45,
customer_weapon: 'Bat',
customer_email: '[email protected]',
...
19
votes
4
answers
27k
views
spread operator converting objects to array
I'm trying to convert a data structure like this:
data = {
0:{A:a},
1:{B:b},
2:{C:c},
}
into a structure like this:
[
{0:{A:a}},
{1:{B:b}},
{2:{C:c}},
]
Using the spread operator ...