destructuring in javascript

Solutions on MaxInterview for destructuring in javascript by the best coders in the world

showing results for - "destructuring in javascript"
Lorenzo
28 Feb 2016
1// destructuring object & nested object & combine object into single object
2let user = {
3  name: 'Mike',
4  friend: ["John", "Paul", "Jimmy"],
5  location: {
6    region:"England",
7    country:"United Kingdom"
8  },
9  aboutMe: {
10    status: "Single",
11    pet: "Dog",
12  }
13}
14
15const { name, friend, location, aboutMe: {status , pet} } = user;
16
17console.log(name); // output: "Mike"
18console.log(friend);  // output: [ 'John', 'Paul', 'Jimmy' ]
19console.log(location); // output: { region: 'England', country: 'United Kingdom' }
20console.log(status); // output: "Single"
21console.log(pet); // output: "Dog"
22
23//Combining Obj
24const newUser = {
25  ...user,
26  car: {
27    make: "Buick",
28    year: 2012,
29  }
30}
31
32console.log(newUser)
33// output user obj + car object into one
34// {
35//   name: 'Mike',
36//   friend: [ 'John', 'Paul', 'Jimmy' ],
37//   location: { region: 'England', country: 'United Kingdom' },
38//   aboutMe: { status: 'Single', pet: 'Dog' },
39//   car: { make: 'Buick', year: 2012 }
40// }
41
42//Bonus destructuring from object of array
43const {friend: [a, ...b]} = user
44console.log(a) // output: "John"
45console.log(b) // output: ["Paul", "Jimmy"]
Maelly
01 Jan 2018
1({ a, b } = { a: 10, b: 20 });
2console.log(a); // 10
3console.log(b); // 20
4
5
6// Stage 4(finished) proposal
7({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
8console.log(a); // 10
9console.log(b); // 20
10console.log(rest); // {c: 30, d: 40}
11
Brooke
28 Jan 2020
1let renseignement = ['voleur' , '10' , 'spécialité'] ;
2
3
4let [classe , force, magie] = renseignement ;
5
6console.log(classe) ;
7console.log(force) ;
8console.log(magie) ;
Annemarie
03 Mar 2018
1const { name, age, job } = { name: 'abrar', age: 24, job: 'web-developer' }
2console.log(name, age, job)
Emilio
07 Feb 2020
1//destructuring in javascript
2const objA = {
3 prop1: 'foo',
4 prop2: {
5   prop2a: 'bar',
6   prop2b: 'baz',
7 },
8};
9
10// Deconstruct nested props
11const { prop1, prop2: { prop2a, prop2b } } = objA;
12
13console.log(prop1);  // 'foo'
14console.log(prop2a); // 'bar'
15console.log(prop2b); // 'baz'
Nikolas
30 Jan 2018
1let {name, country, job} = {name: "Sarah", country: "Nigeria", job: "Developer"};
2
3console.log(name);//"Sarah"
4console.log(country);//"Nigeria"
5console.log(job);//Developer"
6