Swapping variables using array destructuring
? You can use array destructuring to swap values between variables pic.twitter.com/dxdIipgE1M
— Brandon Dail (@aweary) December 28, 2018
Read more [twitter.com]
Posts tagged with destructuring
? You can use array destructuring to swap values between variables pic.twitter.com/dxdIipgE1M
— Brandon Dail (@aweary) December 28, 2018
Read more [twitter.com]
Kent C. Dodds wrote a cool post how an upcoming React feature, called Hooks, works on the hood.
React’s upcoming useState hook relies on array destructuring, let’s dive in and see how that feature works.
https://blog.kentcdodds.com/react-hooks-array-destructuring-fundamentals-952fbfd57ea
Join 9,500+ smart developers
Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
"Freek’s newsletter is one of the best ways to stay updated with the Laravel and PHP ecosystem. It consistently highlights useful packages, tools, and ideas from the community, especially the amazing work coming from Spatie. As a Laravel developer building SaaS and web platforms, I find it extremely helpful to discover practical tools and insights that improve my development workflow."
Frank de Jonge, author of the great EventSauce and Flysytem packages, wrote a blopost on how to use array destructuring in PHP.
In PHP 7.1 the array type has become even more powerful! An RFC was accepted (and implemented) to provide square bracket syntax for array destructuring assignment. This change to the language made it possible to extract values from array more easily.
https://blog.frankdejonge.nl/array-destructuring-in-php/
Not mentioned in Frank's excellent post is PHP's ability to destructure arrays in foreach loops.
$members = [
[1, 'Seb'],
[2, 'Alex'],
[3, 'Brent'],
];
foreach ($members as [$id, $name]) {
// do stuff with $id and $name
}
You can even specify in which variable a value of a specific key should go:
$members = [
['id' => 1, 'name'=> 'Seb', 'twitter' => '@sebdedeyne' ],
['id' => 2, 'name'=> 'Alex', 'twitter' => '@alexvanderbist'],
['id' => 3, 'name'=> 'Brent', 'twitter' => '@brendt_gd'],
];
foreach ($members as ['twitter' => $twitterHandle, 'name' => $firstName]) {
// do stuff with $twitterHandle and $firstName
}
Very neat!
Fav piece of code for today ❤️ Partition collection method, array destructuring, catching multiple exception types... loving PHP + Laravel pic.twitter.com/jkIQkFSzhf
— Jacob Bennett (@JacobBennett) August 24, 2017
Read more [twitter.com]
Ivan Sieder offers a nice tip on how to write better v-for loops in VueJS component templates.
There is really nothing wrong with the above code, but there is a small aspect, which really bugs me. As you can see, we are repeating the product identifier inside the output twice ({{ product.name }} and {{ product.price }}).We are modern web developers (at least I hope so) and therefore we are going to use modern ES6 functionality. The feature we are going to use is called destructuring assignment. Basically, what the destructuring assignment allows us to do, is to pull a specific property off of an object and store it in a variable.
https://simedia.tech/blog/vue-js-destructuring-assignment-for-loops/
Sebastian De Deyne wrote a cool blogpost on array destructuring in PHP. Yet another reason to stay up to date with the latest and greatest PHP version.
PHP 7.1 introduced a new syntax for the list() function. I've never really seen too much list() calls in the wild, but it enables you to write some pretty neat stuff.This post is a primer of list() and it's PHP 7.1 short notation, and an overview of some use cases I've been applying them to.
On dotdev.co Eric L. Barnes explains a new feature that is coming in PHP 7.1
With the accepted proposal it creates an alternative to using “list” for destructuring an array. In all previous versions of PHP this works like this: list($a, $b, $c) = array(1, 2, 3); Now you can extract using a square bracket just as you do for assignment: [$a, $b, $c] = [1, 2, 3]; ["a" => $a, "b" => $b] = ["a" => a, "b", => 2];https://dotdev.co/php-unanimously-approves-short-list-syntax-for-array-destructuring-887208b661af#.58zwoz85l