Linked Questions
80 questions linked to/from Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop
0
votes
1
answer
2k
views
How do I reverse an array in JavaScript while preserving the original value of the original array? [duplicate]
I'm trying to reverse the order of an array using the .reverse() method in JavaScript, but also trying to preserve the original order of elements in the original array. When I save the values into a ...
0
votes
1
answer
5k
views
Copy an array without linking to it [duplicate]
var ProtVar = function(arr){
this.gr1 = [];
this.gr2 = [];
this.arr = arr;
return this;
}
ProtVar.prototype.setArrs = function(){
this.gr1 = this.arr;
this.gr2 = this.arr;
...
-2
votes
1
answer
1k
views
What is the fastest way to copy an array? [duplicate]
What is the fastest way to copy an array?
I decided to make a game, but I feel that Array.filter is too slow, so I created a method:
Array.prototype.removeIf = function(condition: Function): any[] {
...
0
votes
1
answer
132
views
How to maintain parent variable value in javascript [duplicate]
I need one clarification java script. I used two variables in my program.
I stored particular array in first variable and again create one variable.
I have stored second variable value in first ...
0
votes
2
answers
94
views
Array value modified even though it was not modified explicitly [duplicate]
I wrote the code below in an attempt to add an element to the beginning of a new array and return that new array. It works fine, but when I return arr instead of newArr, the arr has changed as well.
...
0
votes
0
answers
50
views
create for every array in array elemet an extra object strange behavior [duplicate]
Why the first example does not give me the expected result, but the second. They are basically the same. My expected console output of processedData2 should be
[
{
"id": "someid&...
0
votes
0
answers
43
views
Are these pointers working behind the scene? Javascript [duplicate]
I am trying to find some questions to answer and found myself in a pinch when trying to answer one.
The question is to duplicate a row whenever a certain value in a column is found. And the duplicated ...
1
vote
0
answers
28
views
Why an array does not reassign in every loop? [duplicate]
Recently I am practicing algorithm questions. There is one that calculates if a string is lexicographically smaller or bigger. I have to find digits, remove them one by one to compare with other ...
3822
votes
83
answers
2.5m
views
How do I correctly clone a JavaScript object?
I have an object x. I'd like to copy it as object y, such that changes to y do not modify x. I realized that copying objects derived from built-in JavaScript objects will result in extra, unwanted ...
2165
votes
40
answers
1.3m
views
Copy array by value
When copying an array in JavaScript to another array:
var arr1 = ['a','b','c'];
var arr2 = arr1;
arr2.push('d'); // Now, arr1 = ['a','b','c','d']
I realized that arr2 refers to the same array as ...
394
votes
17
answers
174k
views
Reverse an array in JavaScript without mutating the original array
Array.prototype.reverse reverses the contents of an array in place (with mutation)...
Is there a similarly simple strategy for reversing an array without altering the contents of the original array (...
126
votes
43
answers
163k
views
Rotate the elements in an array in JavaScript
I was wondering what was the most efficient way to rotate a JavaScript array.
I came up with this solution, where a positive n rotates the array to the right, and a negative n to the left (-length &...
94
votes
11
answers
158k
views
Deep copy an array in Angular 2 + TypeScript
I have an array of objects that is an input. Lets call it content.
When trying to deep copy it, it still has a reference to the previous array.
I need to duplicate that input array, and change one ...
68
votes
11
answers
114k
views
Splice an array in half, no matter the size?
I have an array I would like to split in half. So I can position the first half on the right side and the second half on the left side.
I have used the splice function before:
var leftSide = ...
82
votes
8
answers
221k
views
How to copy all items from one array into another?
How can I copy every element of an array (where the elements are objects), into another array, so that they are totally independent?
I don't want changing an element in one array to affect the other.