javascript

Javascript ES6 Destructuring

const Person = {
  name: "John Snow",
  age: 29,
  sex: "male",
  materialStatus: "single",
  address: {
    country: "Westeros",
    state: "The Crownlands",
    city: "Kings Landing",
    pinCode: "500014",
  },
};

const { address : { state, pinCode }, name } = Person;

console.log(name, state, pinCode) // John Snow The Crownlands 500014
console.log(city) // ReferenceErro

//Extract especif information from objects array
let array = [ {"name":"Joe", "age":17, "sex":"M"},{"name":"Bob", "age":17, "sex":"M"},{"name":"Carl", "age": 35, "sex":"M"}];
let especifArrayAtributes = array.map(x => ({firstname: x.name, age : x.age}));
console.log(especifArrayAtributes); //"[{"firstname":"Joe","age":17},{"firstname":"Bob","age":17},{"firstname":"Carl","age":35}]"
Was this helpful?