Javascript multidimensional array update one value but got all updated [duplicate] - javascript

This question already has answers here:
How to create a 2d array of zeroes in javascript?
(5 answers)
Changing one array element affects other elements
(1 answer)
Closed 1 year ago.
I just found something is interesting in Javascript multidimensional array.
let arr = Array(3).fill([]);
arr[0].push('1');
I thought the result should be arr = [['1'], [], []], but got arr = [['1'], ['1'], ['1']].
If update a value as arr[2][0] = '*', expected array to be arr = [['1'], ['1'], ['*']], but got arr = [['*'], ['*'], ['*']].
So why Javascript multidimensional array work like this? How to just update a value in multidimensional array?

From pichard's comment, I got the right result by doing as following:
let arr = Array(3).fill([]).map(obj => { return [] });
arr[0].push('1');
arr[2][0] = '*';
The result is arr = [['1'], [], ['*']]

Related

how to filter an array by index which is given as an array [duplicate]

This question already has answers here:
How to get an array of values based on an array of indexes?
(6 answers)
Closed 1 year ago.
I want to filter this array by index?
arr = [1,2,3,4,5,6];
I am trying to use
arr.filter(x => x === [2,3])
but it does not seems to work?
I want only [2, 3]rd index?
You need map here rather than filter:
arr = [11,22,33,44,55,66];
indexes = [2,3]
result = indexes.map(i => arr[i])
console.log(result)
Note that this returns values in the indexes order, but if indexes are unsorted and you need values in the source order, then you indeed need filter. Consider:
arr = [11,22,33,44,55,66];
indexes = [4,2]
result1 = indexes.map(i => arr[i])
result2 = arr.filter((_,i) => indexes.includes(i))
console.log(result1)
console.log(result2)

Merge 2 arrays in change [duplicate]

This question already has answers here:
Zip arrays in JavaScript?
(5 answers)
Closed 3 years ago.
I wanted to merge two arrays like you can fold 2 packs of cards - in ugly code with for loop and assuming same length so no if's for safety it would look like this:
const arr = [1,2,3];
const rra = [4,5,6];
const result = [];
for(let i=0; i<arr.length; i++){
result.push(arr[i],rra[i]);
}
console.log(result); // Array(6) [ 1, 4, 2, 5, 3, 6 ]
I know there is something similar in String.raw() but it cuts off last element and returns a string, is there equivalent in array ?
String.raw({raw: [1,2,3]}, ...[4,5,6]); //"14253"
You can use .flatMap() for this - This is the same as .map(), followed by .flat()
const arr = [1,2,3];
const rra = [4,5,6];
let newArray = arr.flatMap((ele, i) => [ele, rra[i]]);
console.log(newArray);

Copy property of each element of array1 into array2 [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 years ago.
I have array1 that contains 1 char in each element.
What I need, is to get the value of array1[i].charCodeAt(); and put it in array2.
Easy to do it, with a for statement.
for(i=0;i<10;i++){
y[i]= x[i].charCodeAt();
}
I did some research but nothing that explain this case:
Is it possible to populate array2 by some sort of destructuring, or what I am asking is not supported in js? For example:
array1 =['a','b','c'];
array2 = [];
array2 = array1[].charCodeAt.
conole.log('The first char has code ' + array2[0]); // The first letter has code 97.
You aren't creating separate standalone variables, so destructuring isn't what you're looking for - but, you can use .map to transform the first array into the second:
const array1 =['a','b','c'];
const array2 = array1.map(char => char.charCodeAt(0));
console.log(array2);
You can use Array.prototype.map():
const array1 = ['a','b','c'];
const array2 = array1.map(c => c.charCodeAt());
console.log(array2);

Get duplicates from array of objects by value [duplicate]

This question already has answers here:
How to combine an array in javascript
(3 answers)
Closed 6 years ago.
I have an array of objects:
var arr = [
{number: "AL-32021611", b: "7500"},
{number: "AL-32021612", b: "Continental"},
{number: "AL-32021612", b: "R3"},
{number: "AL-32021612", b: "7500"}
];
Is there a way that I can get all the number coincidences and get insted of number values the 'b' values in a var?
for example
//loop
result = ["Continental", "R3", "7500"]
what i want is for example i recive the number and then i search all the coincidences with that number value and what i exactly need is all the values from the coincidences
Using ES6 features:
let result = Array.from(new Set(arr.map(el => el.b)));
or
let result = [...new Set(arr.map(el => el.b))];
Array.from()
Set
Array.prototype.map()
Arrow Functions
Spread Operator ...
Str has a nice one-line answer for you, but you can also do it explicitly with a simple for loop. See below.
As you have it,
result = {"Continental", "R3" , "7500"};
is not a valid object. You could use a for loop and push the b values into a new array that would look like:
result = ["Continental", "R3" , "7500"];
Your for loop would look like:
var result = [];
for(var n of arr) {
result.push(arr[n].b);
}
return result;

merging two arrays in Javascript [duplicate]

This question already has answers here:
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 9 years ago.
I am tying to merge two arrays in JS, and then sort them. The following code will output the two arrays on the page, but ONLY if I remove the "newArr.sort();" line. Otherwise, I get nothing. Can anyone help a newbie here?
function merge(arr1, arr2){
var arr1 = [1,21,13,24,15];
var arr2 = [16,7,81,59,14];
var newArr = "[ ]";
arr1.sort();
arr2.sort();
newArr = arr1+","+arr2;
newArr.sort();
document.writeln(newArr);
}
var arr1 = [1,21,13,24,15];
var arr2 = [16,7,81,59,14];
var arr3 = arr1.concat(arr2);
alert(arr3);
Jsfiddle: http://jsfiddle.net/ZRLSs/

Categories