.push() value to JavaScript array (n) times using .fill()?

artomason

Is it possible to .push() a value to an array but replicate the pushed value n times without using a traditional loop to perform the replication? For instance using .fill(). The examples I have seen declare a new Array() with a length of n, and .fill() it with a value. However, I have not seen any examples dealing with .push(), so I'm not even sure it is possible.

Example of what I'm looking for:

var my_array = [];
for (var i = 0; i < 5; i++) {
    my_array.push(5);
};

Scenario:

I'm pulling values from three different arrays or objects to populate a single matrix that will be ran through a Munkres (Hungarian) algorithm, in order to avoid introducing another loop I would like to .push values to the matrix and use .fill() to repeat the value n times.

Example:

var s = […];
var a = […];
var p = […];

var matrix = [];
for (var i = 0; i < s.length; i++) {
    var preferences = [];
    for (var j = 0; j < p.length; j++ {
        var pid = p[j];
        for (var k = 0; k < a.length; k++ {
            if (pid == a[k]) {
                for (var l = 0; l < 5; l++) {  // <-- THIS.
                    preferences.push(a[k]);
                };
            };
        };
    };
    matrix.push(preferences);
};
Jack Bashford

You could use concat and fill:

preferences = preferences.concat(Array(5).fill(a[k]));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Fill input value using javascript

分類Dev

Elegant way to push back std::array to std::vector N times

分類Dev

Fill the object with array in javascript

分類Dev

JavaScript array push not working

分類Dev

Create a javascript object and then fill an array through a for loop

分類Dev

Fill custom SVG image using percentage value

分類Dev

How to get only one value in Javascript array of objects using for of and for in statements?

分類Dev

How to get json value from json array using javascript

分類Dev

Apply a function n times on a starting value

分類Dev

Array(n)とArray(n).fillの違いは?

分類Dev

Getting different value in array what I push

分類Dev

Javascript array value compare

分類Dev

JavaScript Copy Array By Value

分類Dev

Javascript構文:var array = [] .push(foo);

分類Dev

Fill an array "let x = [1..11], iterate over it multiple times in different functions?

分類Dev

Numpy mask array multiple times and fill nans in 3D array with values from another 3D array

分類Dev

Repeat a function call 7 times and then fill labels with the returned values using angularjs

分類Dev

Converting a value using JavaScript

分類Dev

How to fill an input value with pure javascript in a ReactJS stateless component?

分類Dev

array.pushがarray [n] = valueよりも高速な場合があるのはなぜですか?

分類Dev

Example to clarify fill_value when using add() on dataframes (pandas)

分類Dev

Find last nested n times object by its value

分類Dev

Unacceptable value in associative array in Javascript

分類Dev

Max value of a multidimensional array javascript

分類Dev

Sort javascript object with array as value

分類Dev

Using a string array to run one function multiple times in c++

分類Dev

JavaScript: Need a shared array between recursions to push the results to

分類Dev

JavaScript-Array_Push()の有線動作

分類Dev

Javascript:array.pushは未定義です

Related 関連記事

  1. 1

    Fill input value using javascript

  2. 2

    Elegant way to push back std::array to std::vector N times

  3. 3

    Fill the object with array in javascript

  4. 4

    JavaScript array push not working

  5. 5

    Create a javascript object and then fill an array through a for loop

  6. 6

    Fill custom SVG image using percentage value

  7. 7

    How to get only one value in Javascript array of objects using for of and for in statements?

  8. 8

    How to get json value from json array using javascript

  9. 9

    Apply a function n times on a starting value

  10. 10

    Array(n)とArray(n).fillの違いは?

  11. 11

    Getting different value in array what I push

  12. 12

    Javascript array value compare

  13. 13

    JavaScript Copy Array By Value

  14. 14

    Javascript構文:var array = [] .push(foo);

  15. 15

    Fill an array "let x = [1..11], iterate over it multiple times in different functions?

  16. 16

    Numpy mask array multiple times and fill nans in 3D array with values from another 3D array

  17. 17

    Repeat a function call 7 times and then fill labels with the returned values using angularjs

  18. 18

    Converting a value using JavaScript

  19. 19

    How to fill an input value with pure javascript in a ReactJS stateless component?

  20. 20

    array.pushがarray [n] = valueよりも高速な場合があるのはなぜですか?

  21. 21

    Example to clarify fill_value when using add() on dataframes (pandas)

  22. 22

    Find last nested n times object by its value

  23. 23

    Unacceptable value in associative array in Javascript

  24. 24

    Max value of a multidimensional array javascript

  25. 25

    Sort javascript object with array as value

  26. 26

    Using a string array to run one function multiple times in c++

  27. 27

    JavaScript: Need a shared array between recursions to push the results to

  28. 28

    JavaScript-Array_Push()の有線動作

  29. 29

    Javascript:array.pushは未定義です

ホットタグ

アーカイブ