English 中文(简体)
阵列的所有价值
原标题:Javascript | Set all values of an array

<><><>>>>

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;
    }
}

http://jsfiddle.net/alnitak/xG88A/

如果您really希望,请:

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]

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) ] );
}




相关问题
selected text in iframe

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.

How to fire event handlers on the link using javascript

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 ...

How to Add script codes before the </body> tag ASP.NET

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 ...

Clipboard access using Javascript - sans Flash?

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 ...

javascript debugging question

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 ...

Parsing date like twitter

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.

热门标签