Object/Array methods: Modify original or create new? Guidelines - javascript

With javascript we have methods like filter and map which do not modify the original array, but create a new array based on the supplied function.
We also have methods like sort which modify the original array.
The above are examples that are part of ECMA standards.
I also see tailored functions in libraries or GISTs, that do not conform to any standards or best practices.
Things like shuffle, often using algorithms like Fisher–Yates, which 90% of the time modify the original array but occasionally create a new array.
Methods like compress which remove holes in arrays, again often modifying the original array, but on occasion creating a new array.
So while investigating unique or removeDuplicates, I came across 3 possibilities.
function unique(array) {
create newArray;
push unique values into newArray;
return newArray;
}
The above creates a new array of unique values, leaving the original array intact.
function unique(array) {
remove duplicates from original array;
return array;
}
The above modifies the original array as well as returning the reference to the original.
function unique(array) {
create removedArray;
remove duplicates from original array and push the removed elements into removedArray;
return removedArray,
}
The above is similar to the previous except that it records the removed elements and returns them to me, kind of like splice.
This all got me to wondering if there are any standards or best practices when it comes to creating methods that work on Object or Array?
I haven't found any, do they exist?
If not, then I know this question can lead to opinion based answers, but it seems a sensible question to ask?

The ECMAScript Language Specification 5.1 (2011) doesn't outline why some methods modify the original object and why some others do not.
It only outlines, as an example, for Array.prototype.map (15.4.4.19) that
map does not directly mutate the object on which it is called but
the object may be mutated by the calls to callbackfn.
without specifying a reason as to why this is the case.
However, for methods which do modify the original object, no special notice seems to be made outside the general description of the method. An example from the definition of Array.prototype.sort (15.4.4.11):
The elements of this array are sorted. [Rest of the definition follows.]
So to summarize:
In the cases where the original object is not modified, an explicit note is made about it, while in the cases where the original object is modified no special notice is added. Therefore it seems there is an implied standard that such methods modify the original object, however there is no explicit agreement or specification for it.

Related

Why does an array allow a string as an index in JavaScript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
As we already know, one of the differences between an array and object is:
"If you want to supply specific keys, the only choice is an object. If you don't care about the keys, an array it is" (Read more here)
Besides, according to MDN's documentation:
Arrays cannot use strings as element indexes (as in an associative
array) but must use integers
However, I was surprised that:
> var array = ["hello"]; // Key is numeric index
> array["hi"] = "weird"; // Key is string
content structure looks like: ["hello", hi: "weird"]
The content structure of the array looks weird.
Even more, when I check the type of array it returns true
Array.isArray(array) // true
Questions:
Why have this behavior? This seems inconsistent, right?
What is the data structure actually storing behind the scene: as an array or something like an object, hash table, linked list?
Does this behavior depend on a specific JavaScript engine (V8, spidermonkey, etc..) or not?
Should I use arrays like this (keys are both numeric index and string) over normal objects?
An array in JavaScript is not a separate type, but a subclass of object (an instance of the class Array, in fact). The array (and list) semantics are layered on top of the behavior that you get automatically with every object in JavaScript. JavaScript objects are actually instances of the associative array abstract data type, also known as a dictionary, map, table, etc., and as such can have arbitrarily-named properties.
The rest of the section of the MDN docs you quoted is important:
Setting or accessing via non-integers using bracket notation (or dot
notation) will not set or retrieve an element from the array list
itself, but will set or access a variable associated with that array's
object property collection. The array's object properties and list of
array elements are separate, and the array's traversal and mutation
operations cannot be applied to these named properties.
So sure, you can always set arbitrary properties on an array, because it's an object. When you do this, you might, depending on implementation, get a weird-looking result when you display the array in the console. But if you iterate over the array using the standard mechanisms (the for...of loop or .forEach method), those properties are not included:
> let array = ["hello"]
> array["hi"] = "weird"
> for (const item of array) { console.log(item) }
hello
> array.forEach( (item) => console.log(item) )
hello
The MDN statement that "The array's object properties and list of array elements are separate" is slightly exaggerated; the indexed elements of an array are just ordinary object properties whose keys happen to be numbers. So if you iterate over all of the properties with for..in, then the numeric ones will show up along with the others. But as is well-documented, for..in ignores the Array-ness of arrays and should not be used if you're looking for arraylike behavior. Here's another page from MDN that talks about that:
Array iteration and for...in
Note: for...in should not be used to iterate over an Array where the index order is important.
Array indexes are just enumerable properties with integer names and are otherwise identical to general object properties. There is no
guarantee that for...in will return the indexes in any particular
order. The for...in loop statement will return all enumerable
properties, including those with non–integer names and those that are
inherited.
Because the order of iteration is implementation-dependent, iterating over an array may not visit elements in a consistent order.
Therefore, it is better to use a for loop with a numeric index (or
Array.prototype.forEach() or the for...of loop) when iterating over
arrays where the order of access is important.
So to answer your questions:
Why have this behavior? This seems inconsistent, right?
"Why" questions are always difficult to answer, but fundamentally, the answer in this case seems to be that what you are seeing is not an intentional behavior. As far as we can tell, Brendan Eich and the subsequent designers of JavaScript didn't set out to make an array type that also allows non-numeric keys; instead, they elected not to make an array type at all. In JavaScript, there are only eight types: Null, Undefined, Boolean, Number, BigInt, String, Symbol, and Object. Array didn't make the cut — it's not a separate type, but just a class of object. In fact, you could remove arrays from the core language and implement them entirely in JavaScript itself (although you'd lose the convenience of the [...] literal syntax).
So that's why: JavaScript arrays aren't their own type, but just a class of object; all objects in JavaScript are associative arrays; therefore, you can use arrays as associative arrays.
What is the data structure actually storing behind the scene: as an array or something like object, hashtable, linkedlist?
The ECMAScript specification requires objects to function as associative arrays, but does not dictate the implementation; you can assume that they're hash tables without losing any generality. But since the Array class is typically part of the core language implementation rather than pure JavaScript runtime code, I would not be surprised to find that it included special optimizations beyond the generic-object property-handling code for handling the numerically-indexed values more efficiently. As long as the semantics are the same, it doesn't matter.
Whether this behavior depends on a specific JavaScript Engine (V8, SpiderMonkey, etc..) or not?
Different engines might change how Array values are displayed/serialized, and in particular whether such string representations include the non-numeric properties. But the underlying behavior of being able to store arbitrary key–value pairs is a natural side-effect of the language's design and should be universal across all implementations compliant with the ECMAScript specification.
Should I use array like this(keys are both numeric index and string) over normal object?
Well, again, an array is a normal object. But for maximum readability and minimum surprise to those reading your code, I would not recommend using the same object as both a regular and associative array. You could implement a new class that has array-like behavior, maybe even inherit from the Array prototype, but it's usually going to be better to keep those two data structure types separate.
This stumped me recently as well! It seemed to me that if JavaScript allows something like ['a', b: 'c'], it must support associative arrays, so why all the redundancy?
Upon further research, I realized while this does look like an associative array or a JS object…
It's not an associative array, because JS does not actually support them unless we are synonymizing "associative array" and "object".
It IS an array, which is why Array.isArray(array) returned true.
EPIPHANY moment — arrays ✨ ARE ✨ objects. Hear me out 👇🏼
TL;DR
Here are the TL;DR versions of my answers to your questions. For more context and examples, keep scrolling…
On the surface, it does seem inconsistent, but it's actually very consistent when you take a deeper look at the prototype chain and primitive types vs. reference types.
Your array is an array even behind the scenes, but arrays in JS are a type of object and thus can be assigned custom properties in addition to their usual array elements.
Nope! No specific engine needed for this. This is just pure JavaScript.
If you require keys to be numeric and alphanumeric, use an object. HOWEVER, if you would like to use an array, it's perfectly okay to add custom properties to that array as well. Just keep in mind they are only properties of the object, not array elements.
If you require keys, and you also want to maintain the insertion order of your keys (since JS objects cannot reliably retain their property order), consider using a Map object-type instead.
The long version
Almost everything in JavaScript is some sort of object in one way or another, especially if you start exploring the __proto__ chain (MDN docs). In the same way, your array is an array, but it's also an object, just as all arrays are.
However, when you add a value to your array with a key like array["hi"] = "weird", you're not actually pushing a new value to the array, or even a key to the object for that matter in the usual JS objects kind of way, but rather, you are adding a property to the object that the array actually belongs to. So you will still be able to call that property like this:
array.hi // -> "weird"
…but this item is not actually an element in your array, so the below array, even though it's full of properties still only has a length of one.
const array = [];
array.push(1);
array['a'] = 'funky';
array.length; // -> 1
Even more interesting, this only happens when trying to pass an index with an alpha character in it. Even if you pass a numerical string, it will still push the value to the array in the usual/expected way. It's when alpha characters are present that arrays fall back to assigning properties instead of pushing array items.
Stranger yet — you can not only reference these properties without them "counting against" your array length, but you can actually store entire arrays and objects in these properties too as you would any other JS object.
const array = [];
array.push(1);
array['a'] = { b: 'test1', c: ['test2', 'test3']};
array.a.c[1]; // -> "test3"
array.length; // -> 1
Answers to your questions
1. Why have this behavior? This seems inconsistent, right?
Yes and no. This behavior is actually there to be consistent with the object-oriented nature of JavaScript as a whole. At the same time, as you mentioned, this is not consistent with the usual usage of JavaScript arrays and in most cases would likely baffle another developer working on a project where this was used in practice, so I wouldn't advise assigning properties to an array as a means of entry into that array, as it does not function that way.
However, properties are useful in all objects, and it is perfectly safe to add custom properties to any object as long as you are careful not to overwrite existing ones you might need. When you initialize an array [], it has a length of 0, right? Right. But does it already have inherent custom properties? Yep!
If you run the below code, you get ["length"] as a property name that already exists on an empty array.
Object.getOwnPropertyNames([]); // -> ["length"]
Similarly, if you add your own properties, they will also show up when calling this method, along with your array elements:
let array = ['a','b','c'];
array['test'] = true;
Object.getOwnPropertyNames(array); // -> ["0", "1", "2", "length", "test"]
More practically, you may have a time when you want to add a special function to an array in your project but not necessarily want that function to spread across all other arrays. Rather than implementing this as a new prototype function, you can add this new function to a specific array by adding the function as the value of a property as you did in your example. Your properties can even interact with each other.
let array = ['a','b','c'];
array.doubleLength = function() { return this.length * 2 };
array.hasAtLeast = function(x) { return this.length >= x };
array.length; // -> 3
array.doubleLength(); // -> 6
array.hasAtLeast(1); // -> true
array.hasAtLeast(3); // -> true
array.hasAtLeast(4); // -> false
2. What is the data structure actually storing behind the scene: as an array or something like object, hashtable, LinkedList?
I discussed this in more detail above as well. Essentially, what is happening here is that the new properties are being stored as object properties, but not in the usual object { key: value } key-value pair sort of way. Arrays and objects are both actually different types of what we'll refer to here as "raw objects". Everything in JavaScript is some instance of a raw object in one way or another. Arrays are a type of "raw" object. Key-value pair objects are a type of object and are also not these "raw" objects we are talking about, but another manifestation of them.
All objects shared a few key attributes. One of those attributes is that they all share the same ability to store properties, which we normally recognize as the keys in the objects we work with on a daily basis. All objects allow the addition of custom properties.
Some types of objects like strings require you to traverse to their prototypes in order to add properties. Because of this, this won't work:
let string = "Hello World";
string.addExclamation = function() { return this + "!" };
string.addExclamation() // -> THROWS ERROR!!!
…but this WILL work:
let string = "Hello World";
string.__proto__.addExclamation = function() { return this + "!" };
string.addExclamation(); // -> "Hello World!"
Other types of objects including both key-value objects and arrays, allow you to add custom properties directly to them without traversing to their prototypes, as I mentioned in my answer to your first question above.
This distinction between which types of objects allow the direct adding of properties and which don't, and which we'd consider "raw" objects and which we wouldn't boils down to a JavaScript ideology known as "primitive types" and "reference types". I highly suggest reading up on that "primitive vs. reference" distinction more.
It's also useful to note how to set up these prototype properties and the difference between prototype and __proto__. In many ways, prototype and __proto__ work the same, the key difference being that you can call the __proto__ property on any variable/object to get the prototype for its object type. On the other hand, prototype can be called only on the actual constructor for a variable/object. The below two prototype and __proto__ lines actually call the same object.
const string = "";
string.__proto__; // -> String { … }
String.prototype; // -> String { … }
string.__proto__ === String.prototype; // -> true
3. Whether this behavior depends on a specific Javascript Engine (V8, SpiderMonkey, etc..) or not?
Nope! This is about as raw as it comes when working with JavaScript. Just the beauty of object-oriented programming. I explain this in a lot more depth in the above two answers to your questions, as well as the content I added before my answers.
4. Should I use an array like this (keys are both numeric index and string) over a normal object?
Contrary to what a lot of answers are saying, yes 👏🏼 do use arrays like this IF your situation calls for it. I don't mean to contradict myself here. Earlier, I did say you may want to avoid this, and that's true, so allow me to elaborate.
Should you add elements to arrays like this, where it adds them as properties and not as array elements? No. Adding to arrays like this is actually adding JS properties to them, not array elements.
Is there ever a case where I SHOULD add properties to arrays like this? Yes! It may not be often, and I explain this in more detail in my response to your first question, but if you are in a situation where you want to add custom properties, even functional properties to one specific array and not the Array.prototype which would affect all arrays, this is a perfectly fine and safe way to do so. I'll add my same example from my answer to question #1 below for reference:
let array = ['a','b','c'];
array.doubleLength = function() { return this.length * 2 };
array.hasAtLeast = function(x) { return this.length >= x };
array.length; // -> 3
array.doubleLength(); // -> 6
array.hasAtLeast(1); // -> true
array.hasAtLeast(3); // -> true
array.hasAtLeast(4); // -> false
For further reading, I would highly encourage reading my post in its entirety and chewing on it, and then checking out the MDN docs on the prototype chain. For even further study, I highly suggest signing up for Just JavaScript, a completely free 10-part email digest series and takes you on a deep dive into JavaScript Mental Models, which focuses largely on this prototype chain and other fascinating parts of what makes JavaScript so robust. It's written by Dan Abramov, one of the co-creators of Redux, and beautifully illustrated by Maggie Appleton. Definitely worth a peek!
Other answers look good.
In JavaScript, an array is an object.
For example, you can destructure an array as an object due to each item in an array is an object with key-value pairs.
const array = ["Hello", "World"];
const {0: first, 1 :second} = array;
console.log({first, second});
And in case that the keys of each item in an array including integer, string, etc. You can object destructure by a specific key.
var array = ["hello"];
array["hi"] = "weird";
const {
0: first, // integer --> index number
hi: second // string --> string key
} = array;
console.log({first, second});
Besides, you can only access an item by index when key is an integer.
var array = ["hello"];
array["hi"] = "weird";
console.log(array[0]); // hello
console.log(array[1]); // undefined
console.log(array["hi"]); // weird
Why does array allow string as an index in JavaScript
To add custom behaviour to an object
Question 1.1
Why have this behavior?
This is because array is itself an object with specific behaviour.
Array is a data structure which defines the behaviour of the object. So when we declare array we tell the compiler that this object must be treated as an array so the compiler only considers numeric indexes in an array.
For an array:
let array = []
If you write like this:
array["hi"] = "weird";
The compiler treats it as a property of array and in array, all properties are ignored by default
length is one of the properties of an array which returns it's length.
Since we know that length is a property of array. Try running the below program you'll get the idea.
let array = [];
array[0] = "first";
array[1] = "second";
array[2] = "third";
array[3] = "fourth";
array["hi"] = "weird";
// we overrided a predefined property of an array. You can try changing it's length and see what happens
array["length"] = 3;
console.log(array, '\n Length of array: ', array.length);
Question 1.2
This seems inconsistent, right?
No, what if we want to add custom behaviour to the array. However, it can be done usnig 'prototype` but it applies for all the arrays but for specific array it might be needed. However, it is never needed.
Question 2
What is the data structure actually storing behind the scene: as an array or something like object, hashtable, linkedlist?
It is stored as an object since it is a data structure
Question 3
Whether this behavior depends on a specific Javascript Engine (V8, SpiderMonkey, etc..) or not?
Behaviour of this won't change for javascript operations but handling of operations may differ.
If you run the above snippet in Console of Dev Tools in Chrome, the hi index is printed whereas it doesn't in the case of the Firefox and stackoverflow because they use different engines
You can read more about it here
Question 4
Should I use array like this(keys are both numeric index and string) over normal object?
No, it doesn't make sense to use array to store values in a (string)key when you cannot use the string keys in methods like forEach, map and array related methods. It'll make things complicated but still if you want to do it then use an object instead.
You can read more about it here
Answer to question 1 and 2
Arrays are so-called "Integer Indexed Exotic Objects" in ECMAScript which are based on which in return belong to the object data type. Exotic objects are objects with some special behavior such as integer indices in our case. As far as I can tell, since we set a string index, our array is no longer an integer indexed exotic object.
Answer to question 3
Since this is ECMAScript, my understanding is, that all JavaScript engines have to implement it this way to stay compatible with the specification.
Answer to question 4
You should definitely not. Arrays should only have integer indices as per spec ("Integer Indexed Exotic Objects"). If you want to use string or mix both, use an object. When using an object, integer indices are coerced to string indices.
Note that this will only work in Chrome or in any other V8 engine browser. I tested your code in Safari and it didn't store the "hi" key-value pair at all. But in Safari it is rather an exception than the rule since you don't get an error either.
The reason why it works is that it is still an object. The functionalities of arrays are stacked on top of what objects in general have.
typeof array // "object"
Edit
By the advice of #Kevin B, I realized that Safari indeed doesn't handle it differently than Chrome. The console.log implementation must be different in both browsers in this regard. I was still able to access the key value pair even if it wasn't shown when printing the array in the console.
Also I noticed that the OP is looking for an answer from a reputable source. The only reputable source that I came across which exactly reflects what I wrote above is from w3schools.com
Arrays are Objects
Arrays are a special type of objects. The typeof operator in
JavaScript returns "object" for arrays.
But, JavaScript arrays are best described as arrays.
Arrays use numbers to access its "elements". In this example,
person[0] returns John:
[...]
In JavaScript, there are two categories of data types: primitive types (number, string, boolean, bigInt, symbol, undefined, null) and objects (also known as Reference types).
An array is a special object that uses integers as its keys. JavaScript still treats it as an object. That means you can add/remove properties (That array will be called associative array or dictionary, like your example). If you want to truly treat it as a standard array, you should never use strings as keys.
Furthermore, you should use arrays as standard arrays because in JavaScript arrays have behavior and optimizations specific to their intended use. Also, when you add a new property with a string key to your array, the value of length will not change.
var array = ["hello"]; // Another way to declare a array is array = new Array("Hello"). This version clearly shows array is just a object.
array["hi"] = "weird";
console.log(array.length) // 1 not 2
array[1] = "A.Phong"
console.log(array.length) // 2

How can I make object properties in console.log(obj) display alphabetically in Chrome?

It used to be that, when I used console.log(obj) to display an object's properties in Javascript, the properties would display alphabetically. Recently, this seems to have changed. I assume that the new order is closer to how the properties are actually stored in the code, (though I was under the impression that object properties didn't have a "real" fixed order to begin with).
However, for large, complex classes, the alphabetical display was very useful for finding the property that I was looking for. Is there an option for making the properties display alphabetically, as before?
(I would also like to know what exactly the current order of properties "represents" and how it is determined in the first place.)
though I was under the impression that object properties didn't have a "real" fixed order to begin with
They do, as of ES2015 (and even operations that previously were exempt are being brought in line now), but you probably want to continue to act as though they don't. For own properties, it's the order they were added to the object (except for integer index properties which are listed in numeric order before other properties); details in the answers to this question. (There is no defined order for inherited properties, although engines are fairly consistent about what they do with them: They list them after own properties, following the same order rules. But again, that's not specified.)
Recently, this seems to have changed.
Looking at it experimentally, it looks like it's following property order now.
You could give yourself a utility function that creates a new object with the properties added to it in alphabetical order:
function logAlpha(obj) {
const x = {};
for (const key of Object.keys(obj).sort()) {
x[key] = obj[key];
}
console.log(x);
}
Of course, unlike console.log(obj), that will be a snapshot, not a live link to the original object.
You can do a lot to tweak that (including the prototype properties, including the prototype itself, including sorted versions of the full prototype chain, etc.), but that's the gist.

[].slice.call(this) vs array.prototype.call(this)

Just wondering what's the different with the following js script.
[].slice.call(this) vs Array.prototype.slice.call(this);
They seems to be doing this same, can someone explain the different and which one i should stick with?
Cheers
They are almost identical. Let's examine each of them:
[].slice.call(data)
What it does?
[] - this creates new Array, same to new Array()
slice - it retrieves method slice from array
call(data) - it calls slice() method replacing its current context with data variable, i.e. uses data as its internal array, not []. This returns our data converted into Array.
Now, the second one:
Array.prototype.slice.call(data)
This one:
Array.prototype.slice - retrieves method slice from Array.prototype; in a nutshell - it returns you slice method without any context (internal data).
call(data) - as in previous variant, calls this method with data as its context.
Conclusion
Both [].slice and Array.prototype.slice return you a slice function which you can call on some array, however this array is passed not as argument, but as context (using call method of function object). In JavaScript, these calls will be absolutely the same:
// Calling as a method of 'Hello' string
'Hello'.indexOf('e') // returns 1
// Calling as unbound method
String.prototype.indexOf.call('Hello', 'e') // returns 1
String.prototype.indexOf.apply('Hello', ['e']) // returns 1
Those methods are almost the same. First one is more readable, but second one uses a bit less memory because it isn't creating a temporary initial Array.
I believe that Array doesn't actually create an object, where as [] is an instance of Array.
Definitely stick with the latter one as the first one is an implicit array construction and the same as:
new Array().slice.call(this)
Its constructing a new array that needs to be garbage collected afterwards since you dont use it. While both statements "work", the above mentioned does a lot of extra work, first you construct a new instance, then you look up if the instance itself has a slice property, as this is false the prototype chain is traversed and the slice is found on the Array.prototype and then called. Then you call it with your other array as scope, so essentially render the instance you created useless by using your other one.
Thats why Array.prototype.slice.call(myarr) is the proper way of accessing the slice method.
Array.prototype.slice.call() seems slightly more performant, though that makes sense because it would be quicker at runtime to "look up" this method. I posit that [].slice.call() is the best choice. Here's the jsperf of reference
Let's be real, in what case would the negligible difference between the two be a major contributor to poor website performance? sounds like heaven, actually. The shorter syntax of the literal method is awesome. I'd rather save myself the half-second it'd take to type the prototype variant than be shakily more efficient.
The coffin nail of Array.prototype... some people say is that it's irresponsible because you risk altering it. But... that's stupid. Figure 1: Array.prototype = function() { console.log('foo') }; Whoopsie... That probably happens all of the time. Seriously, even granting its legitimacy is still intact, it takes me longer to type and is imo hideous compared to its sexier literal sister.
The real answer to your real question is that the functionality is exactly the same. Use whichever you will still appreciate on your deathbed.

.slice.call vs .push in javascript

One of my friends uses [].slice.call() to fill an array with matched elements.
I wonder how this works. I just know the Array.prototype.push to fill an array.
Also I have seen there is a difference in both techniques to fill an array.
So my questions are:
How does Array.prototype.slice help to fill an array?
What are the roles of Array.prototype.slice and Array.prototype.push to make objects and arrays?
Fiddle
var arr=[];
var arr=[].slice.call($('[id^=name]'));
//arr.push($('[id^=name]'))
console.log(arr)
The .slice() method of arrays returns a shallow copy of a portion of an array. It's written as a method on the Array prototype, so the array it operates on is the array in whose context it's invoked. Normally that'd be something like this:
var newArray = oldArray.slice(2, 4);
When you call the .slice() method with .call(), you can control the value of this explicitly. In your friend's code, he's passing a jQuery object as the this value. Because there are no other parameters, .slice() returns a shallow copy of the jQuery object in its entirety, as a new array. (Like some other similar methods, .slice() will work on anything that looks like an array: basically, anything that has a .length property and numerically-indexed properties of interest. A jQuery object fits that description.)
However, since jQuery already has a built-in method to return a plain array of all matched elements, your friend should not bother doing that anymore:
var plainArray = $('[id^=name]').get();
That does exactly the same thing. Using .slice() isn't wrong, but it's kind-of pointless.
How about $('[id^=name]').get()?
The [].slice.call method leverages the array-like length and numeric key properties of jQuery objects to re-use the intentionally generic methods of Array.prototype. The method Array.prototype.slice is documented by the standard to return a new Array object by performing a specific algorithm that accesses numeric-keyed properties in sequence of the this object - regardless of its type - assigning the values to sequential elements of the new Array.
I wouldn't use either approach, because jQuery has get(), which is documented to return an array of the elements matched. Why use a more obscure or less direct approach?
I does't have any specified reason for this case, but The thing i know is, .slice() also used to create another instance of an array, And here the slice add a space to its instance because it have no parameters.
Javascript is the language where you perform a Single operations in 1000's of different ways ! its depend on your choice!
But, We must have to follow the syntax that is made for particular operation.

How to make filterFilter return an array of indexes instead of items?

I want to use filterFilter functionality but I want to reuse the arrays of data instead of creating a new array everytime it runs. So returning an array of indexes and then I can, inside my service, return the items associated to the indexes. This way I can create a $watch export on my service, so I can watch on the real items instead of the filters.
I considered copying the angular filterFilter source, but that isn't maintainable in the long run, what could I do?
Context:
I'm writing a form wizard service that applies a schema to the steps (there are many with slightly different object structures each step), that holds an array of objects. filterFilter returns a new array of objects, so when I modify the object inside the service using a "setter", it's not updating the object itself, but a new (?) created object, and that's no use for me
Javascript has Object.keys( arrayName ) to do just that, but you'll have to include a poly-fill for IE8 or lower.
var a = ['foo', 'bar'];
Object.keys( a ); //returns ['0','1'];
The MDN Object.keys article has an article for implementation if you want to use this with older browsers.
However, upon looking at your problem I think you misread the filter filter. When you use this filter it may return a different array than the input, however every object in that filter is still a reference to an object in the original. So editing any object should modify the original without a problem.
Hope this helps.

Categories

Resources