Destructuring Array
Wed Jun 21 2023 12:27:49 GMT+0000 (Coordinated Universal Time)
Saved by
@sarfraz_sheth
#react.js
const animals = [
{ name: "cat", sound: "meow", foodRequierements: {
food: 2,
water: 3
} },
{ name: "dog", sound: "woof" }
];
const [catName, dogName] = animals // is taking the animal array and assign a variable name for each item
const {name, sound} = catName // is going deeper and is taking the value of the name key of the first item of the array which is catName NOTE: is importante that the varibale name must be equal to the key names of the array.
const {name: catName, sound: catSound} = catName // rename the keys to be used individually of each item
const {name = "Fluffy", sound = "Purr"} = catName // define a default value incase the value is null or empty
const {name, sound, foodRequierements} = catName // get the keys of the object. but bring the foodRequierements as object, to acces its value you will need to use dot . and call the property
const {name, sound, foodRequierements: {food, water} } = catName // to call the key of the nested object this is the bes method
console.log(sound)
content_copyCOPY
diferentes formas de hacer el destructuring de un arreglo
Comments