How to Create an Array Containing 1...N

How to create an array containing 1...N

If I get what you are after, you want an array of numbers 1..n that you can later loop through.

If this is all you need, can you do this instead?

var foo = new Array(45); // create an empty array with length 45

then when you want to use it... (un-optimized, just for example)

for(var i = 0; i < foo.length; i++){
document.write('Item: ' + (i + 1) + ' of ' + foo.length + '<br/>');
}

e.g. if you don't need to store anything in the array, you just need a container of the right length that you can iterate over... this might be easier.

See it in action here: http://jsfiddle.net/3kcvm/

How to create an array containing 1...N

If I get what you are after, you want an array of numbers 1..n that you can later loop through.

If this is all you need, can you do this instead?

var foo = new Array(45); // create an empty array with length 45

then when you want to use it... (un-optimized, just for example)

for(var i = 0; i < foo.length; i++){
document.write('Item: ' + (i + 1) + ' of ' + foo.length + '<br/>');
}

e.g. if you don't need to store anything in the array, you just need a container of the right length that you can iterate over... this might be easier.

See it in action here: http://jsfiddle.net/3kcvm/

How to create an array containing 1...N

If I get what you are after, you want an array of numbers 1..n that you can later loop through.

If this is all you need, can you do this instead?

var foo = new Array(45); // create an empty array with length 45

then when you want to use it... (un-optimized, just for example)

for(var i = 0; i < foo.length; i++){
document.write('Item: ' + (i + 1) + ' of ' + foo.length + '<br/>');
}

e.g. if you don't need to store anything in the array, you just need a container of the right length that you can iterate over... this might be easier.

See it in action here: http://jsfiddle.net/3kcvm/

Generate numbers from 1 to 100 in an array in a way no number is repeated

you can use map

const array = Array(100).fill(1).map((n, i) => n + i)

console.log(array)

How to create an array from 1 to N in TypeScript?

If you want an array from 1 to N, you can create a new array of length N, fill it with a filler value, then populate the values using their indices with .map.

For example:

const n = 10;
const myArray = new Array(n).fill(null).map((_, i) => i + 1);

with result for myArray:

[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

However in this case, it seems you don't need that array if you're just using it alongside another array. Instead you can use the index from that other array (second_array) as values for the "name" key.

Like this:

const newArray = secondArray.map((e, i) => ({
name: i + 1,
value: e,
}));

Example case:

Input

const secondArray = [100, 200, 300];

Result (for newArray)

[
{ name: 1, value: 100 },
{ name: 2, value: 200 },
{ name: 3, value: 300 }
]

ES6 - generate an array of numbers

Here is a simple solution that works in codepen:

Array.from(Array(10).keys())

To be clear, Array.from() and Array.keys() require an ES6 polyfill in order to work in all browsers.

Create a new array of all values from an array with step

numpy can not be used for this purpose as len(ecg_property.sample) #115,200 is not fully divisible by number_samples_by_duration_exp_temp #10,000 and numpy cannot allow elements of varying lengths :)

You can try list comprehension.

result_list = [ecg_property.sample[temp :temp+step] for temp in np.arange(times)*step ]

where
step=10000 and times = len(ecg_property.sample) / step

It can be further modified if needed and as per requirement.

(You can try out each step in above line of code in this answer and see the output to understand each step )
Hope this works out.
ty!

How to create a new array based on the number of sub arrays in js

you can do something as the following code:

const generate = (numbers, letters) => {
return numbers.map((nums, i) => {
return nums.map(num => letters[i])
}).flat()
}

And then use it with generate(withSub, arr).

How create array containing all combinations of array 1-n array items?

It's just a simple recursive problem....

const
passengerFlights =
{ 777: [ { _id: 'aaa' }, { _id: 'bbb' }, { _id: 'ccc' } ]
, 888: [ { _id: 'ddd' } ]
, 999: [ { _id: 'eee' }, { _id: 'fff' } ]
}
, result = combinations( passengerFlights, '_id' )
;

console.log( showArr(result) )

function combinations( obj, KeyName )
{
let
result = []
, keys = Object.keys(obj) // [ "777", "888", "999" ]
, max = keys.length -1
;
f_recursif_combi(0)
return result

function f_recursif_combi( level, arr = [] )
{
obj[ keys[level] ] // like :passengerFlights['777']
.forEach( elm =>
{
let arr2 = [...arr, elm[KeyName] ]; // arr + elm['_id']
(level < max)
? f_recursif_combi(level +1, arr2 )
: result.push( arr2 )
})
}
}

// ************************************ just to present result...
function showArr(Arr)
{
const r = { '[["': `[ [ '`, '","': `', '`, '"],["': `' ]\n, [ '`, '"]]': `' ]\n]` }
return JSON
.stringify(result)
.replace(/\[\[\"|\"\,\"|\"\]\,\[\"|\"\]\]/g,(x)=>r[x])
}
.as-console-wrapper {max-height: 100%!important;top:0 }


Related Topics



Leave a reply



Submit