JavaScript Destructuring - javascript

I was looking for info on JavaScript destructuring and found the video "Destructuring Assignment" as part of a video series from Packt Publication. At the very beginning of the video, I saw the following code:
var [a, b] = [1,2,3];
a === 1;
b === 3;
The presenter then explains why variable b is 3 and not 2, which didn't seem correct to me, but I thought maybe I'm wrong.
So I did a Code Pen with the following code:
var [a, b] = [1,2,3]
console.log(a,b) //1 2
As I expected, the variable b is 2.
Is there something I'm missing and not understanding?
Below is a screenshot of the video in questions.

Yes, the video is wrong, these below are the only ways to get the 3 in this array (using two variables names):
const [a, , b] = [1, 2, 3]; // b is 3
const [a, ...b] = [1, 2, 3]; // b is [2, 3], so b[1] is 3
Also, see: Destructuring to get the last element of an array in es6

Related

Reference of array inside another array (not an item of the other array)

This is probably a dumb question but whatever.
If have two arrays like this:
let a = [0,1,2];
let b = [0,1,2,3,4,5];
Is it possible that I can make changes in one happen in the other array as if they are the same place in memory but different lengths like you could in C? (in javascript)
What i want:
a[0] = 1;
console.log(b);
//[1,1,2,3,4,5];
console.log(a);
//[1,1,2];
Not with plain arrays (unless using getters/setters or proxies to copy the values). However, a shared memory like in C is possible with typed arrays and their subarray method:
const b = Uint8Array.from([0, 1, 2, 3, 4, 5]);
const a = b.subarray(0, 3);
console.log(a, b);
a[0] = 1;
console.log(a, b);
The downside (?) is that you can store only integers in the array, not objects or strings.

how to get values from an json array

Guys this might be a simple question, but please help.
i have a data.
var a = { "data":[[1,2,3],[2,4,3],[3,6,7],[1,4],[6,4,3,4],[6,7,3,5]] }
i'm plotting a multiple line chart using chartjs and i want these valus in array to use as datasets.
what i wat is to save each array in different var's
like;
var a = [1,2,3],
var b = [2,4,3]
var c = [3,6,7]
so that i can pass theese values to chart js and plot chart. any help is appreciated. i thought of foreach and getting by each position. but its not working.
regards
Use Array destructuring.
Use spread operator on last node. That will keep all the remaining nodes except the specified number of paramaters, if you are interested on the frst three nodes only.
var data = { "data": [[1, 2, 3], [2, 4, 3], [3, 6, 7], [1, 4], [6, 4, 3, 4], [6, 7, 3, 5]] }
const [a, b, c, ...restNodes] = data.data;
console.log(a);
console.log(b);
console.log(c);
console.log(restNodes);
Please Note Its not mandatory to have the last node with spread operator. You can pick the first three nodes only using
const [a, b, c] = data.data;
I just said you can do this aswell
Spread the data and just assign to three variables.
var x = { "data":[[1,2,3],[2,4,3],[3,6,7],[1,4],[6,4,3,4],[6,7,3,5]] }
let [a,b,c] = [...x.data];
console.log(a);
console.log(b);
console.log(c);
There is no need to even include a fourth variable if all you care about is a,b,c.

JavaScript destructuring with an array element

I have met the following questions in JavaScript:
const [x1, ...[result]] = [3, 4, 5]
console.log([result])
I know x1 is 3, but why is the logging result [4] instead of [4,5]?
So basically what is happening if we follow this syntax
const [a,...b] = [3,4,5]
Javascript creates an array called b and has the value [4,5]
But in your case what is happening is,
const [a,...[b]] = [3,4,5]
This is essentially assigning to only the first variable of the empty array with first value as b, which always equals 4 and not [4,5] as you expect.
So it's equivalent to the case below
const [a,...[b,c]] = [3,4,5]
the only difference is that you are not providing a variable c in your case.
So b would correspond to 4 and c would correspond to 5

Problem with Accesing array data Javascript

I want to access data of var a so it is: 245 but instead it only accesses the last one. so if i print it out it says 5
var A = [1, 2, 3, 4, 5];
var B = A[[1], [3], [4]];
console.log(B)
When accessing an object using square bracket notation — object[expression] — the expression resolves to the string name of the property.
The expression [1], [3], [4] consists of three array literals separated by comma operators. So it becomes [4]. Then it gets converted to a string: "4". Hence your result.
JavaScript doesn't have any syntax for picking non-contiguous members of an array in a single operation. (For contiguous members you have the slice method.)
You need to get the values one by one.
var A = [1, 2, 3, 4, 5];
var B = [A[1], A[3], A[4]];
console.log(B.join(""))
var A = [1, 2, 3, 4, 5];
var B = [A[1], A[3], A[4]];
console.log(B)
You'll need to access A multiple times for each index.
var A = [1, 2, 3, 4, 5];
var B = A[1];
console.log(A[1], A[3], A[4])
You can access them directly like that.
If you want to access index 2 for example, you should do console.log(A[1]);
You can't access multiple indices at the same time.
A variable can have only one value.
#Quentin solution resolve the problem, I wrote this solution to recommend you to create an array of index, and iterate over it.
Note: You are getting the last index, because you are using the comma operator. The comma operator allows you to put multiple expressions. The resulting will be the value of the last comma separated expression.
const A = [1, 2, 3, 4, 5];
const indexes = [1,3,4];
const B = indexes.map(i => A[i]).join``;
console.log(B);

Javascript copying an array of arrays [duplicate]

This question already has answers here:
Create copy of multi-dimensional array, not reference - JavaScript
(4 answers)
Closed 8 months ago.
I want to copy an array of arrays at a different allocation.
I know that I can copy an array in the following way:
a = [[1, 2], [3, 4]]
b = a.slice() // this makes sure that a and b are allocated differently in memory
Now if I change something inside b, then of course,
b[0] = 'abc'
console.log(a, b) // expect a = [[1,2], [3,4]] and b = ['abc', [3,4]]
But when I do the below, a gets changed as well...!
b[0][0] = 'abc'
console.log(a, b) // now it gives a = [['abc', 2], [3, 4]] and b = [['abc', 2], [3, 4]]
Why is this happening, and how can I avoid mutating a?
Thanks so much!
If you know you are only copying 2D arrays you could use a function like the following and avoid using JSON:
function copy2D(array){
result = []
array.forEach((subArray) => {
result.push(subArray.slice())
})
return result
}
One way would be by using map combined with the spread operator. This would be the easiest approach if you can assume that you have a 2D array only
const a = [[1, 2], [3, 4]]
const b= a.map(item => ([...item]))
b[0][0]= "abc"
console.log('a:', a, 'b: ', b)

Categories

Resources