0
let chosen = 3;
    let myFriends = [
      { title: "Osama", age: 39, available: false, skills: ["HTML", "CSS"] },
      { title: "Ahmed", age: 25, available: false, skills: ["Python", "Django"] },
      { title: "Sayed", age: 33, available: true, skills: ["PHP", "Laravel"] },
    ];

    // let [x,y,z]=myFriends;
    // let {title:ft,age:fag,available:fav,skills:[,fskill]}=x;
    // let {title:st,age:sag,available:sav,skills:[,sskill]}=y;
    // let {title:tt,age:tag,available:tav,skills:[,tskill]}=z;

    switch (chosen) {
      case 1:
        let [d]=myFriends;
        let {title,age,available,skills:[,skill]}=d;
        console.log(title);
        console.log(age);
        console.log(available?"available":"notAvailable");
        console.log(skill);
        break;
      case 2:
        [,d,]=myFriends;
        ({title,age,available,skills:[,skill]}=d);
        console.log(title);
        console.log(age);
        console.log(available?"available":"notAvailable");
        console.log(skill);
        break;
      case 3:
        [,,d]=myFriends;
        ({title,age,available,skills:[,skill]}=d);
        console.log(title);
        console.log(age);
        console.log(available?"available":"notAvailable");
        console.log(skill);
        break;
      
    }

I tried to change variables name , but every time it tells me that variable is not intialized (I mean variable (d) ) and tells me that variables (title , age skills) cann`t be reached

pilchard
  • 12,414
  • 5
  • 11
  • 23
  • 2
    Toy have to declare your variables one every block because variables are block-scoped and if one case runs the other don't so variables are not declared in other cases. Just add `let` before the other variables as well – Konrad Apr 16 '23 at 06:46
  • ...or create a block for each `case`: `case 1: { let d /* ... */ }`. Though, in this particular example, you should simply use `myFriends[d - 1]` instead of `switch`/`case`. – InSync Apr 16 '23 at 07:16
  • this issue happens because of of the scope of the variables in `switch-case`. – saeede-gh Apr 16 '23 at 07:21
  • 1
    Does this answer your question? [ES6 block scoping with in switch](https://stackoverflow.com/questions/38828898/es6-block-scoping-with-in-switch) – pilchard Apr 16 '23 at 08:27
  • 1
    also [switch statement and scopes in ES2015](https://stackoverflow.com/questions/39001827/switch-statement-and-scopes-in-es2015) – pilchard Apr 16 '23 at 08:28

1 Answers1

2

Declaring the variables within each switch case did not work for me when destructuring data. However, it works to declare the variables as common to all the switch cases.

let chosen = 3;
    let myFriends = [
    { title: "Osama", age: 39, available: false, skills: ["HTML", "CSS"] },
    { title: "Ahmed", age: 25, available: false, skills: ["Python", "Django"] },
    { title: "Sayed", age: 33, available: true, skills: ["PHP", "Laravel"] },
    ];

    // let [x,y,z]=myFriends;
    // let {title:ft,age:fag,available:fav,skills:[,fskill]}=x;
    // let {title:st,age:sag,available:sav,skills:[,sskill]}=y;
    // let {title:tt,age:tag,available:tav,skills:[,tskill]}=z;
    let d;
    let title,age,available,skill;

    switch (chosen) {
    case 1:
        [d]=myFriends;
        ({title,age,available,skills:[,skill]}=d);
        console.log(title);
        console.log(age);
        console.log(available?"available":"notAvailable");
        console.log(skill);
        break;
    case 2:
        [,d,]=myFriends;
        ({title,age,available,skills:[,skill]}=d);
        console.log(title);
        console.log(age);
        console.log(available?"available":"notAvailable");
        console.log(skill);
        break;
    case 3:
        [,,d]=myFriends;
        ({title,age,available,skills:[,skill]}=d);
        console.log(title);
        console.log(age);
        console.log(available?"available":"notAvailable");
        console.log(skill);
        break;
    }
Saranya
  • 36
  • 4