How can I create an array containing 1...X without loop [duplicate] - javascript

This question already has answers here:
How to create an array containing 1...N
(77 answers)
Closed 20 days ago.
how creating a JavaScript array containing 1 through to x where x is only known at runtime without the loop.
var arr = [];
for (var i = 1; i <= x; i++) {
arr.push(i);
}

This is a duplicate of this issue
here is the answer anyway
const arr = [...''.padEnd(N)].map((_,i)=>i+1)

Related

Fastest and Most Efficient way to check for duplicates in a javascript array [duplicate]

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
In Javascript, how do I check if an array has duplicate values?
(9 answers)
Checking for duplicate strings in JavaScript array
(13 answers)
Closed 5 months ago.
I am writing a javascript function that takes a nested array and returns the numbers that occurs more than once in that array.
I believe my function is accurate (meaning that it passes their "Correctness test" ) but i am after efficiency, how efficient is this code?
For example - Lets call the name of the function deepSort(nestedArray) where nestedArray is the nested array parameter
function deepSort(nestedArray) {
const flatArr = nestedArray.flat().sort();
let results = []
for (let i = 0; i < flatArr.length - 1; i++) {
if (flatArr[i + 1] == flatArr[i]) {
results.push(flatArr[i]);
}
}
return (results.filter((item, index) => results.indexOf(item) === index)).join()
}
const a = deepSort([[1,3,4,5], [4,7,9,1,3], [2,3,5], [1,2,3,4]]) // Returns 1,2,3,4,5
console.log(a);
const b = deepSort([[1,2,3], [4,5], [6,7,8], [2,9,0]]) // Returns 2
console.log(b);
const c = deepSort([[2,7,9], [4,3], [9,6,5], [1,4,3]]) // Returns 3,4,9
console.log(c);
Can this code be optimized any more for speed and efficiency when handling extremely large values of data?

When uniquely updating an object in an array in Javascript, the objects values are all changing [duplicate]

This question already has answers here:
How to update one Javascript object array without updating the other [duplicate]
(3 answers)
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 3 years ago.
I currently have an array of objects called posts.
for(var i = 0; i < posts.length; i++){
let post = posts[i]
let { item, category } = post
let postCollections = categories[category]
for(var col in userCollections[category]){
let items = userCollections[category][col].items
if(items && col){
postCollections[col]['item'] = item
console.log("HERE!, item)
if(item in items){
postCollections[col]['joined'] = true
}else{
postCollections[col]['joined'] = false
}
}
}
posts[i]['collections'] = postCollections
}
When this is run, the print out for "HERE!" shows the item value is unique. When I print out posts and look at the value for key items they all show the same item.
This was a tough solve. Turns out the line where I set postCollections was using the same object over and over again. Copying the object like this has done the trick:
let postCollections = JSON.parse(JSON.stringify(categories[category]));

How to merge and add inner array of numbers in javascript? [duplicate]

This question already has answers here:
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 3 years ago.
I am working with a multi-dimensional array.
the following is my array:
let arr = [[1,2,3],[1,2,3],[1,2,3]];
the length of the inner arrays will always be the same.
I want create a function to add all the arrays elements together with their respective elements and create a new array with the result.
so my desired output is
result =[3,6,9];
You can use nested for loop for that.
let arr = [[1,2,3],[1,2,3],[1,2,3]];
let res = Array(arr[0].length).fill(0);
for(let i = 0;i<arr[0].length;i++){
for(let j = 0;j<arr.length;j++){
res[i] += arr[j][i]
}
}
console.log(res)

Why is array = Array(10).fill(Array(20).fill(-1)) not the same as array [[-1,..,-1], ..., [-1,..,-1]]? [duplicate]

This question already has answers here:
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 4 years ago.
I tried to create a 2-dimensional array in javascript. (Actually, the code is written in typescript and will be used to style DOM Elements with Angular2 but, as far as I am concerned, it shouldn't have any effect on the result in this case.)
First way:
arrayi = Array(10).fill(Array(20).fill(-1))
Second way:
array = [];
for(var i: number = 0; i < 10; i++) {
array[i] = [];
for(var j: number = 0; j< 20; j++) {
array[i][j] = -1;
}
}
If I call array[1][2] = 2; it does what I expected, what means setting one element in the 2-dimensional array to 2.
However, if I call arrayi[1][2] = 2; every element arrayi[x][2] will be set to 2. (With x element {0,1,2,...,9})
Why is that? I don't understand it. So it would be nice if someone could explain it to me.
The arrays are defined as array: number[][] and arrayi: number[][] in typescript.
arrayi = Array(10).fill(Array(20).fill(-1)) fills with the same array.
You can even rewrite it like this
const arr1 = Array(20).fill(-1)
const arrayi = Array(10).fill(arr1)
console.log(arrayi.every(item => item === arr1)) //true
The reason is that you create one instance of array with 20 elements and put this single instance into each element of root array

Javascript: Setting all array values to the same [duplicate]

This question already has answers here:
Initializing an Array with a Single Value
(12 answers)
Closed 8 years ago.
I want to make a new array (of X elements) with all the elements set to 0.
X is set before.
Anyone could make the most compact and easy code for that? Thank you for your
help.
Just create a function:
function makeArray(size, defaultValue) {
var arr = [];
for (var i = 0; i < size; i++) arr.push(defaultValue);
return arr;
}
var myArr = makeArray(10, 0);

Categories