Javascript: how to join two arrays [duplicate] - javascript

This question already has answers here:
Javascript Array Concat not working. Why?
(7 answers)
Closed 6 years ago.
I know this has been asked a lot of times, but I cannot get it to work.
I have an empty array a
var a = [];
and an array with an object b
var b = [{
title: 'test'
}]
I want to join them so a will look exactly like b.
The idea is to do this inside a for loop so a would be added a new item each time.
By using a.concat(b), a results in an empty array.
Not sure what I am missing.

Per Array.prototype.concat()
This method does not change the existing arrays, but instead returns a new array.
You need to assign this operation back to a
a = a.concat(b)

You need to assign result of that call to a. a = a.concat(b)

Related

How to merge two arrays at run time [duplicate]

This question already has answers here:
Why Doesn't Array Concatenation Work in Javascript? [closed]
(3 answers)
Closed 2 years ago.
I am getting below like values at runtime. Below is for only example. I will have same array structure at runtime. I want to merge them.
let finalSearchResult =[];
data [{"a":1000,"a":1000001,"a":10000002,"D":5000000}]
data [{"P":1000,"Q":1000001,"R":10000002,"S":5000000}]
finalSearchResult.concat(finalSearchResult,data);
but its not working. When I am printing finalSearchResult its coming as null.
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
please refer to:
Array.prototype.concat documentation
Therefore:
finalSearchResult = finalSearchResult.concat(data);
Will be the correct way to concat the arrays.

Why does it change the array(argument) value in a function, when I already passed the array(argument) values to another one to modify? [duplicate]

This question already has answers here:
Copy array by value
(39 answers)
Closed 3 years ago.
So I need the original(argument) array to stay the same, AND have another array (array2) with it's values to make some changes on it but mantain the original(argument) array intact.
Example:
let wtf = function(array){
let array2 = array
array2[0] = array2[0].replace(array2[0][0],"1")
console.log( array + " " + array2)
}
wtf(["a","b"])
Result in console:
1,b 1,b
BUT I need a,b 1,b
(that comes from: array = a,b and array2 = 1,b )
Thanks!
I think this is a reference value vs a clone value problem.
With your let array2 = array line, I think you're creating a reference to the same in-memory object, so you're actually modifying a single array twice, even though it looks like you've created another one.
To create a 'true' clone you can try something like:
let array2 = JSON.parse(JSON.stringify(array));

Reverse Array in TypeScript is too fast [duplicate]

This question already has answers here:
Weird behavior with objects & console.log [duplicate]
(2 answers)
Reverse array in Javascript without mutating original array
(15 answers)
Closed 3 years ago.
I have a little problem with TypeScript. I want to reverse an Array. But before I reverse my Array, I want to write in into a local Storage.
The problem is, that I call the method to store the data before I call the method to reverse my array. But the stored data are already reversed.
Here is an example code and here is the Playground:
class TSRunner {
defaultArray = []
reversedArray = [];
fillArray() {
this.defaultArray = [1, 2, 3, 4, 5];
this.reversedArray = this.defaultArray;
console.log(this.reversedArray);
this.reversedArray.reverse();
}
}
let runner = new TSRunner();
runner.fillArray();
The output is already 5,4,3,2,1. But I want to get the 1,2,3,4,5 and after that, I want to reverse it. Maybe you could explain me this behaviour. I think it has something todo with the call by reference stuff.
Thanks before ;)

JavaScript 2D array push when empty [duplicate]

This question already has answers here:
Initializing and filling multi dimension array in javascript
(2 answers)
Closed 4 years ago.
So I have been trying to create an empty 2d array and trying to push values into it.
The following code is as follows.
// Create an array like [[],[]]
let series = new Array(2).fill([]);
//try to push value into the array in position 0 like [[5],[]]
series[0].push(5); // output here is [[5],[5]]
How can I push element to the array at index 0?
There is something I'm missing. Any help is appreciated!!
When you use fill([]) it creates one array and fills everything with a reference to that one array. You can use something like map() or Array.from() which will create a new array object with each iteration:
let series = Array.from({length: 2}, () => []);
series[0].push(5);
console.log(series)
Use [...Array(2)].map(a=>[]); instead.
When you use fill you assign the same object reference to everything
let series = [...Array(2)].map(a=>[]);
series[0].push(5);
console.log(series)

Assigning multiple indices at once from one list [duplicate]

This question already has answers here:
JavaScript Object Literals & Array Literals
(4 answers)
Closed 5 years ago.
I have five user variables with different names. I'd like them to end up in an array, and am wondering if I can do the assignment to multiple array indices in a single line. Currently, if I do this
var users = {user5k, user10k, user15k, user20k, user25k};
I can't index it later (says users[0] is undefined). So I want the indices built in to the assignment. Something along these lines:
var users = {};
users[0:4] = {user5k, user10k, user15k, user20k, user25k};
This doesn't seem possible in JavaScript, meaning I have to do this:
var users = {};
users[0] = user5k;
users[1] = user10;
// ... et cetera
Is there a way to accomplish the first solution?
When you use {} you're creating an object, not an array. Use [] for arrays.
var users = [user5k, user10k, user15k, user20k, user25k];
If you want to replace part of an array that already exists, you can use Array.prototype.splice()
var users = [];
users.splice(0, 4, [user5k, user10k, user15k, user20k, user25k]);

Categories