why is [[],[],[]] different than new Array(3).fill([])? [duplicate] - javascript

This question already has an answer here:
If we create an array of objects using new Array(len).fil({}); and then add a key in any of the objects, it gets reflected in all the 3 objects
(1 answer)
Closed 4 years ago.
So my question is the same as in the title. I saw that someone initiated a similar thing here (If we create an array of objects using new Array(len).fil({}); and then add a key in any of the objects, it gets reflected in all the 3 objects) but while everyone accused the improper method of questioning nobody gave a clear answer.

Array(3).fill([]) creates 3 elements referencing the passed object.
The answer is clear in javascript documentation:
The fill() method fills all the elements of an array from a start
index to an end index with a static value

It seems to me like the only answer in the question you linked to is perfectly clear.
Array.fill "fills" the array with static values. That is to say, you are creating on single empty array and then referencing it in all three of the locations, hence it is the same array in each of the outer array's indices

Related

Firestore update nested data [duplicate]

This question already has answers here:
How can I update an object inside of an array in firestore?
(1 answer)
How to update an "array of objects" with Firestore?
(18 answers)
Is there any way to update a specific index from the array in Firestore
(3 answers)
Closed 8 months ago.
i am stuck with a problem with firebase (with Vue). I want to add the user id to a specific map (yes, maybe or no) when the user presses a specific button. But Im having trouble add/update the data because its nested.
Here is my data structure in Firestore
I want to add the user id in a map, something like this:
dates: {
[
0: {
yes: [userId1, userId2]
}
]
}
Does anyone can help me pushing the user ids into the arrays?
Unfortunately, right now it is not possible to update a specific element of the array. You can add new items or remove them - look here. But I would consider a little change in the approach and create a collection called dates in which you can define documents, which will contain yes, no, maybe arrays, which can be easily updated - in the way mentioned before.

What means an empty element in an array and why it has taken into account in the length if theoretically it does not exist [duplicate]

This question already has answers here:
Memory allocation of a Javascript array?
(3 answers)
Closed 4 years ago.
I add a element to the array in a higher position than its length
I wonder if is there a way to access or delete this empty elements, I think it can waste memory
When doing this, you create a "sparse array". The items in between the non-null entries contain undefined. As such, the memory footprint is negligible. You cannot delete them. If you don't like this behavior, just don't create sparse arrays.
For filtering non sparse/dense elements, you could use a callback which returns for every element true.
Maybe this link helps a bit to understand the mechanic of a sparse array: JavaScript: sparse arrays vs. dense arrays.
let array = new Array(99999),
nonsparse;
array[30] = undefined;
nonsparse = array.filter(_ => true);
console.log(nonsparse);
console.log(nonsparse.length);

Why overwrite element of 2D array in javascript [duplicate]

This question already has answers here:
Copy array by value
(39 answers)
Closed 6 years ago.
I have a 2D array "allcolor" in javascript. So that I initialize each row of it with a 1D array same below code.
var allcolor=[];
var color=["#f73214","#f5f714","#54f714","#141df7","#006400","#556B2F","#2F4F4F",
"#808080","#8FBC8F","#4B0082","#9400D3","#483D8B","#1E90FF","#00BFFF",
"#FFFF00","#ADFF2F","#9932CC","#FF69B4","#FF1493","#8B0000","#8B4513",
"#B22222","#CD5C5C","#E9967A","#FF8C00","#DAA520","#F0E68C","#FFFAF0",
"#000000"];
for(p=0;p<x.length;p++){
allcolor[p]=color;
}
When I change each element of each row of allcolor, overwrite other element of all row of allcolor. When I run this bellow code, I see that all [..][0] and all [..][4] are same value '#000000' and '#ffffff'.
console.log(allcolor);
allcolor[0][0]='#000000';
console.log(allcolor);
allcolor[0][4]='#ffffff';
console.log(allcolor);
How can I change value of one row?
So, this happens, because javascript variables only store references to memory places where objects belong (kind of like pointers in C++). When you put color in the allcolor array 4 times, then they'll refer to the same object, thus if you change the value of one row, you change every row's value.
To avoid this, do
allcolor[p]=color.slice();

What is the most efficient way to find an element in an array? [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 7 years ago.
I get an array of objects from the backend as this one, currently only three elements but in the future will be more.
[{"code":"lng.dropdown.home","text":"Inicio"},
{"code":"lng.dropdown.shop","text":"Tienda"},
{"code":"lng.button.support","text":"Soporte"}]
What is the most efficient way in javascript to find an element in this array by code, and return the text associated? Can we use lambdas expresions here?
you can use array filter to find the nested object(s) that matches your code property:
var arr =
[{"code":"lng.dropdown.home","text":"Inicio"},
{"code":"lng.dropdown.shop","text":"Tienda"},
{"code":"lng.button.support","text":"Soporte"}];
var res = arr.filter(function(x){
return x.code == 'lng.dropdown.home'
})
console.log(res[0])
[].filter returns an array with the objects that return true inside the callback.
As far as efficiency is concerned for loop are proved to be faster but using higher order methods you earn abstraction .
When not using other libraries, I usually map then find the indexOf, as follows:
data[data.map(function(a){ return a.code }).indexOf("code I am trying to find")];
This returns the element of data at the index where code matches the code you're trying to find.
When I have lodash as a dependency, I usually just do this however:
_.find(data, {code:"code I am trying to find"})
(I think that's the proper syntax)
Edit:
Didn't realize you were already using JQuery, the answer using $.grep is probably best.

how to merge the values of two objects into one using javascript [duplicate]

This question already has answers here:
Merge JS objects without overwriting
(4 answers)
Closed 9 years ago.
I am working in jQueryMobile and PhoneGap.
I Have 2 objects: eup and gld. The length of eup is 22, and the length of gld is 6.
I have tried:
//common.push(eup,gld);
//common.join(eup,gld);
//common.concat(eup,gld);
alert(common.length) // 2
When I check common.length, it's 2.
But for my logic I need it as 1. That means merge eup and gld and shows its length as 1. I got the result of eup and gld from two different APIs and it's in JSON format.
The main thing is the tags of both objects are identical. So I think it should be possible to merge these values as one and show its length as ONE.
Is there any solution for this????
It sounds like you want to extend an object, not concatenate an array.
var common = $.extend({}, eup, gld);
However, this will overwrite values with common keys. To merge without overwriting, the solution is explained at Merge JS objects without overwriting.

Categories

Resources