I want to clone a new array from an existing one and insert an element. Unfortunately, the existing array is empty, so it likes:
[].slice().push(65)
the output of the above expression is 1.
Why is this 1?
Array#push()returns the length of the resulting array. Since you are pushing a single value on an empty array, the length of the result will indeed be 1.
Array#push()
If you want to see the updated array as output, you need to save a link to it, since it push()does not return a link:
push()
var arr = [].slice(); arr.push(65); console.log(arr); // [ 65 ]
Editing my comment on the answer:
MDN push(): push() .
, .
var temp = [].slice(); temp.push(65); console.log(temp);
, concat()
concat()
var a = [1,2,3]; var b = [].concat(a,64); console.log(b);
Source: https://habr.com/ru/post/1539768/More articles:Эффективные операции столбца (сумма, среднее...) на очень большом Enumerable с Linq - c#Availability of errno module in python on different platforms - pythonjqGrid: mess when cellattr returns a string containing "style" - jqueryonbeforeunload - Can I get a reason for the call? I want to identify a redirect - javascriptNaN treatment - pythonQList with nested types does not display properly in VS debugger - c ++SDK is not suitable for Android - javaHow to effectively use the event bus? - javaUsing route express node.js but express.Router, getting as undefined - javascriptSort list in progressive numerical order - for-loopAll Articles