<><><>>>>
var cool = new Array(3);
cool[setAll] = 42; //cool[setAll] is just a pseudo selector..
alert(cool);
<>Result>
警告信息:
42,42,42
我如何将阵列的所有价值改变/确定为具体价值?
<><><>>>>
var cool = new Array(3);
cool[setAll] = 42; //cool[setAll] is just a pseudo selector..
alert(cool);
<>Result>
警告信息:
42,42,42
我如何将阵列的所有价值改变/确定为具体价值?
如果没有内在的,你就不得不放弃:
function setAll(a, v) {
var i, n = a.length;
for (i = 0; i < n; ++i) {
a[i] = v;
}
}
如果您
Array.prototype.setAll = function(v) {
var i, n = this.length;
for (i = 0; i < n; ++i) {
this[i] = v;
}
};
http://jsfiddle.net/alnitak/ee3hb/。
但是,有些人在扩大建筑型的原型上fr。
http://www.ohchr.org。 ES5引进了一条途径,可安全地扩展<代码>。 反对:prototype 和Array.prototype
,但不打断for ...... in
Object.defineProperty(Array.prototype, setAll , {
value: function(v) {
...
}
});
<EDIT 2 在ES6草案中,现在还有<代码>Array.prototype. 填满/编码>,使用<代码>cool. 填满(42)。
ES6办法非常干净。 因此,首先,你开始使用一系列x年限,然后使用<代码>填满/代码>方法。
let arr = new Array(3).fill(9)
this will create an array with 3 elements like:
[9, 9, 9]
这个问题的最符合逻辑的解决办法是:
let xs = [1, 2, 3];
xs = xs.map(x => 42);
xs // -> [42, 42, 42]
但是,如果阵列有可能消失,则需要将<>条码>用于<<>/条码>,或更佳的<条码>用于<>。
见:
1920年,你应利用这一方法:
www.un.org/chinese/ga/president
基本上,<代码>20为length
或新的即时编码
使用<条码>,以条码>取代,然后逐一列出。
实际上,你可以采用这一完美的方法:
let arr = Array.apply(null, Array(5)).map(() => 0);
// [0, 0, 0, 0, 0]
This code will create array and fill it with zeroes. Or just:
let arr = new Array(5).fill(0)
其他答案是奥基,但似乎更为合适:
function setAll(array, value) {
var i = array.length;
while (i--) {
array[i] = value;
}
}
A more creative version:
function replaceAll(array, value) {
var re = new RegExp(value, g );
return new Array(++array.length).toString().replace(/,/g, value).match(re);
}
不能在任何地方工作。
在与流行病做事时创建,这显然是行之有效的,因为我看不到什么。
/** Convert a set of picture points to a set of Cartesian coordinates */
function toCartesian(points, scale) {
const x_max = Math.max(...points.map(p=>p[0])),
y_max = Math.max(...points.map(p=>p[1])),
x_min = Math.min(...points.map(p=>p[0])),
y_min = Math.min(...points.map(p=>p[1])),
signed_x_max = Math.floor((x_max - x_min + 1) / 2),
signed_y_max = Math.floor((y_max - y_min + 1) / 2);
return points.map(p=>
[ -scale * (signed_x_max - p[0] + x_min),
scale * (signed_y_max - p[1] + y_min) ] );
}
您可使用 empt/a>。
const arr = [1, 2 ,3];
for (let i = 0; i < arr.length; arr[i++] = 42);
console.log(arr);
How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.
I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...
Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...
Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...
I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.