Git Product home page Git Product logo

Comments (18)

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by guy on 08/27/2019 15:35:32

Uhm, a little slip here:
Array.from(new Set(['one', 'two'])); // => ['one', 'two', 'three']
Unless there's a new feature I'm unaware of :)

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Dmitri Pavlutin on 08/27/2019 15:48:09

Nice catch! I will fix the example.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Joshua Baker on 08/28/2019 00:23:47

In 4.2, your example's lambda function is () => value, but value was not defined. Did you mean init?

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Patrick John Haskins on 08/28/2019 06:52:29

I'm typing from my phone, so I can't test this to be sure, but I believe your deepCopy() in #3 will not perform a true deep copy. It will effectively deep copy multi-dimensional arrays of immutable values. When it encounters an object, though, it will only copy the reference to the object.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Dmitri Pavlutin on 08/28/2019 07:03:44

Yep, it will deep copy only the array objects.
I updated the text to be more clear.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Dmitri Pavlutin on 08/28/2019 07:03:51

Fixed.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Yevgen Gorbunkov on 08/28/2019 15:07:31

Wouldn't most of the above use cases be solved in a bit more concise manner with
spread operator (...)?

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Dmitri Pavlutin on 08/28/2019 15:15:10

What exact use cases are you referring to? Please elaborate.

You can only use spread operator as an alternative of Array.from() to clone an array or to remove duplicates.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Yevgen Gorbunkov on 08/29/2019 15:44:38

Double object property values; Set/Map/NodeList into array; Shallow array copy; Fill array with values/objects; Range of numbers; Unique entries

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Yevgen Gorbunkov on 08/30/2019 12:36:58

Check out this collection of codepens

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Dmitri Pavlutin on 08/30/2019 13:31:58

Thanks for providing the demos. Sure, you can use spread operator as an alternative if you'd like.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Masha I. Klachko-Blair on 09/27/2019 19:15:27

To me, spread operators are super unreadable. Array.from() is much easier to read and understand.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Dmitri Pavlutin on 09/28/2019 14:04:28

It's a matter of taste. I'm ok with using the spread operator.
But I like to use Array.from() when I need the map function of it.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Waheed Afolabi on 10/07/2019 10:23:50

Nice article. It's really helpful.

Seems Array.from( ) clones an array including nested ones without using the recursiveClone() function...


const nestedArray = [[1, 2, 3], ['one', 'two', 'three']];


function recursiveClone(val) {
return Array.isArray(val) ? Array.from(val, recursiveClone) : val;
}
const nestedArrayCloneUsingFunc = recursiveClone(nestedArray);


const nestedArrayClone = Array.from(nestedArray);


nestedArrayCloneUsingFunc; // [[1, 2, 3], ['one', 'two', 'three']]
nestedArrayClone; // [[1, 2, 3], ['one', 'two', 'three']]

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Dmitri Pavlutin on 10/07/2019 11:17:35

Array.from() does only a shallow copy of the array.

Regarding your example: nestedArray[0] === nestedArrayClone[0] equals to true, which means that nestedArrayClone is a shallow copy of nestedArray.

from dmitripavlutin.com-comments.

panzerdp avatar panzerdp commented on May 28, 2024

Comment written by Waheed Afolabi on 10/08/2019 15:54:18

Oh! Now I know and understand the difference...thanks!

from dmitripavlutin.com-comments.

joker628 avatar joker628 commented on May 28, 2024

Could you plz explain, the parameters passed in Array.from() in
#5-generate-ranges-of-numbers.

from dmitripavlutin.com-comments.

ManuelRCastroIglesias avatar ManuelRCastroIglesias commented on May 28, 2024

Thank you!
It was something I was looking for and couldn't find.
I hope to try everything thoroughly soon.

from dmitripavlutin.com-comments.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.