Dynamically create js Array filled with certain value [duplicate] - javascript

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 7 years ago.
I'm wondering if there is a way to create javascript/jquery array in one-liner to receive something like:
my_array = ['-', '-', ,'-' ,'-']
Idea is that array should be created with dynamic length and all values filled with given value.
Thanks in advance!

Try:
var total = 4;
var my_array = new Array(total + 1).join("-").split("");
document.write(JSON.stringify(my_array))
.fill Support The native function will be added in (ECMAScript 6), but for now is not available.
if(!Array.prototype.fill){
Array.prototype.fill = function(val){
for (var i = 0; i < this.length; i++){
this[i] = val
}
return this
}
}
var my_array = new Array(4).fill("-"); //returns ["-","-","-","-"]

Try to use:
Array.apply(null, new Array(63)).map(String.prototype.valueOf,"-")

Related

Javascript push and loop [duplicate]

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 2 years ago.
I currently have this array: var arr = []
How do I push multiple "hello" strings into the array using a for loop?
I tried
var newArray = arr.push("hello")10;
try new Array forEach or simple for-loop should work.
var arr = [];
// method 1
new Array(5).fill(0).forEach(() => arr.push("hello"));
// alternate method
for (let i = 0; i < 5; i++) {
arr.push("world");
}
console.log(arr);
// Updating based on suggesion #mplungjan, quick way without loop.
var arr2 = Array(10).fill("hello");
console.log(arr2)

JavaScript matrix wired behaviour when using new Array() [duplicate]

This question already has answers here:
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 4 years ago.
When i use Array.fill to fill a multidimensional array, i get a weird behaviour when pushing to one of the arrays:
var arr = Array(2).fill([]);
arr[0].push(5);
console.log(arr);
//=> prints [[5], [5]]
fill is essentially doing this:
var content = [];
for (var i = 0; i < 2; i += 1) {
arr[i] = content;
}
So, your array will have a reference to the array you've passed to fill in each property.
It sounds weird, but what your code actually does is create an array ([]) and put a reference for that array in each of the items of the Array(2). So whenever you change that reference - every array that is referenced to that Array is changed.
It's exactly the same as:
var a = [];
var arr = Array(2).fill(a);
a.push(5);
console.log(arr[0][0], arr[1][0]);
a[0] = 2;
console.log(arr[0][0], arr[1][0]);
You can see that the values inside the arr are affected by the change to the a array.

Weird behaviour in Array.fill [duplicate]

This question already has answers here:
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 4 years ago.
When i use Array.fill to fill a multidimensional array, i get a weird behaviour when pushing to one of the arrays:
var arr = Array(2).fill([]);
arr[0].push(5);
console.log(arr);
//=> prints [[5], [5]]
fill is essentially doing this:
var content = [];
for (var i = 0; i < 2; i += 1) {
arr[i] = content;
}
So, your array will have a reference to the array you've passed to fill in each property.
It sounds weird, but what your code actually does is create an array ([]) and put a reference for that array in each of the items of the Array(2). So whenever you change that reference - every array that is referenced to that Array is changed.
It's exactly the same as:
var a = [];
var arr = Array(2).fill(a);
a.push(5);
console.log(arr[0][0], arr[1][0]);
a[0] = 2;
console.log(arr[0][0], arr[1][0]);
You can see that the values inside the arr are affected by the change to the a array.

How to remove duplicated objects from an Array in javascript [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 6 years ago.
I am using above code and removing the items based on id
I am getting id is not defined while comparing i+1 item when i reaches to maximum array length.
Appreciate your help....
var arr = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":474.5,"top":16},{"id":"0-block-2","left":686.5,"top":0}];
Expecting outout = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":686.5,"top":0}]
Array.prototype.unique = function(){
var passNum = this.length;
var _self = this;
// to repeat loops n*n times
while(passNum>0){
for(var i = 0;i<this.length;i++) {
if(this[i].id==this[i+1].id){
var _indx = this.indexOf(this[i]);
this.splice(_indx,1);
}
}
passNum = passNum-1;
}
return this;
};
You can do this with ES2015 Sets.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
EDIT: If you can't use ES2015, there is a polyfill available.

how to create array object using integer variable javascript [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I need to fill an array object as like below using integer variable.
for(i=1; i<=2; i++)
arr[i].push({i:(100 * i)})
Expected result is:
arr = [{ 1:100,2:200},{1:100,2:200}]
Problem is, array created as like below
arr = [{i:100,i:200},{i:100,i:200}]
You need to push to arr instead of arr[i].
Also, you can't use a variable as a key in json directly.
var arr = [];
for(i=1; i<=2; i++)
{
var b = {};
b[i] = 100*i;
arr.push({[i]:(i*100)});
}
console.log(arr);

Categories

Resources