Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is copying an array called "slice"? [closed]

Tags:

javascript

I know what as slice of strawberry cake is and if you're talking about data I know that you can slice that up, and get shards or buckets.

I don't get it though, why in js land copying data is called "slice". What is the underlying reason behind this choice in naming?

Edit: the trouble for me was to realise that the word "slice" should be taken literaly (a realisation that would have led me to think this method is overloaded). English is not my native language and many times, wordings in API´s should not be taken literaly. More precisely, the word might have several meanings (definitions) that point in different directions (such as the keyword "map", which means several things).

Edit2: when folks talk about data, especially when it's big, they usually throw around words such as "map", "reduce", "aggregate" and "slice". Here's from pinterest´s blog, where they explain their big data storage solution and the meaning of the word "slice" seems to follow javascript´s thought of that same word:

"There are hundreds of Hadoop jobs slicing the data across multiple dimensions to produce reports that track our business metrics and generate derived aggregates that feed into our serving infrastructure."

...meaning, they have an original array of data that they slice to get new copies of that data. The original is not mutated.

My confusion and misconception of the terminology might be shared by others. Close this Q otherwise. Your call.

like image 229
Marcus Avatar asked Oct 21 '25 11:10

Marcus


2 Answers

In fact, slice was designed to get a slice out of the array. It is just a convenient way to tag along this function instead of creating another new function to copy the entire array.

See: http://www.ecma-international.org/ecma-262/7.0/index.html#sec-array.prototype.slice

like image 181
some1 Avatar answered Oct 23 '25 01:10

some1


It's because .slice will slice array from starting index to end index provided.

When copying array you use myArray.slice(). It means you start from 0th index and continue till the end. Make one big slice of array.

like image 44
Justinas Avatar answered Oct 23 '25 00:10

Justinas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!