1

Is there a JavaScript Version of the Python expression in the form of

nums = [0,0] + [1] * (n)

?

n is the number of times an item is repeated. Examples: If n is 3, nums produces [0,0,1,1,1], If n is 4, nums produces [0,0,1,1,1,1] and if n is 5, nums produces [0,0,1,1,1,1,1] and so on.

7
  • 5
    What does that expression produce in Python? Without knowing this, it's kind of hard to give an equivalent operation in JS. Commented Dec 3, 2020 at 14:30
  • So, [1] * (3) produces [1,1,1] and then [0,0] + [1,1,1] makes [0,0,1,1,1], correct? Commented Dec 4, 2020 at 16:36
  • Yes it does make [0,0,1,1,1]. Commented Dec 4, 2020 at 16:37
  • 1
    Can you edit the question to add that information there, please? I've voted to reopen but ideally the question should contain all information, instead of having to look at the comments. Commented Dec 4, 2020 at 17:03
  • 1
    Your question basically consists of two questions. How to concatenate arrays and how to create an array of repeated elements. Put together you get [0,0].concat(Array(3).fill(1)). Commented Dec 4, 2020 at 17:07

2 Answers 2

1

In JavaScript there aren't operators defined that work on arrays, however, you can imitate their results:

Operations

List concatenation (+)

This is very simple as there is the Array#concat() method

a = [1, 2]
b = [3]

c = a + b

in JavaScript is equal to

const a = [1, 2];
const b = [3];

const c = a.concat(b);

console.log(c);

You could define a function that concatenates any amount of elements as:

const add = (...items) => [].concat(...items);

console.log("[1, 2] + [3]:", add([1, 2], [3]));
console.log("[1] + [2] + 3:", add([1], [2], 3));
console.log("[1, 2] + 3:", add([1, 2], 3));
console.log("1 + 2 + 3:", add(1, 2, 3));

List Repetition (*)

There is no native way to repeat an array in JavaScript. You can apply a loop and repeatedly call add, however an easier way might be to use Array.from. The first argument it takes is an array-like which can be used to define the size of the create {length: neededSize} and the second argument it takes is a function that will determine the contents of this array.

This is a simple implementation:

const mul = (arr, times) => 
  Array.from({length: times*arr.length}, (_,i) => arr[i%arr.length]);

console.log("[] * 3:", mul([], 3));
console.log("[1] * 3:", mul([1], 3));
console.log("[1, 2] * 3:", mul([1, 2], 3));
console.log("[1, 2, 3] * 3:", mul([1, 2, 3], 3));

Module that handles operations fluently

For the sake of it, here is a small module that makes the operations into a fluent interface and eagerly evaluates each operations:

const add = (...items) => 
  [].concat(...items);
const mul = (arr, times) => 
  Array.from({length: times*arr.length}, (_,i) => arr[i%arr.length]);

const arrayMath = arr => ({
  add: (...items) => arrayMath(add(arr, ...items)),
  mul: num => arrayMath(mul(arr, num)),
  result: () => arr
})

const res1 = arrayMath([])
    .add([1, 2])
    .add([3])
    .mul(2)
  .result();

const res2 = arrayMath([])
    .add(1, 2)
    .add([3])
    .mul(2)
  .result();

const res3 = arrayMath([])
    .add(1, [2])
    .add([3])
    .mul(2)
  .result();


const res4 = arrayMath([1])
    .mul(2)
    .add(2)
    .mul(3)
  .result();


console.log(res1);
console.log(res2);
console.log(res3);
console.log(res4);

Sign up to request clarification or add additional context in comments.

Comments

0

Since when you simply multiply a number to an array, it will parse all the contents in array to integer to perform the operation, but while parsing it also considers the comma that separates the elements in the array, so ultimately the parse gives you a NaN(Not a Number).

So you have to do it manually by a for loop(or any loop):

var arr = [0,0];

 function myFunction() {
  let x;

  for(x=0;x<3;x++){
  arr.push(1)
 }

Change the variables according to your wish later.

2 Comments

Anytime, always here to help. Did it help though?
Yes it did help. But you can also use let arr = [0.0].concat(Array(3).fill(1)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.