JavaScript - get array element fulfilling a condit

2020-02-17 08:56发布

I'm learning JavaScript using W3C and I didn't find an answer to this question.

I'm trying to make some manipulations on array elements which fulfill some condition.

Is there a way to do it other than running on the array elements in for loop? Maybe something like (in other languages):

foreach (object t in tArray)
   if (t follows some condition...) t++;

another thing, sometimes I want to use the element's value and sometimes I want to use it as a reference. what is the syntactical difference?

As well, I'll be happy for recommendations on more extensive sites to learn JavaScript from. thanks

9条回答
趁早两清
2楼-- · 2020-02-17 09:30

About arrays

What you usually want for iterating over array is the forEach method:

arr.forEach(function(el) {
  alert(el);
});

In your specific case for incrementing each element of array, I'd recommend the map method:

arr = arr.map(function(t){ return t+1; });

There are also filter, reduce, and others, which too come in handy.

But like Tim Down already mentioned, these won't work by default in IE. But you can easily add these methods for IE too, like shown in MDC documentation, or actually you can even write simpler versions than the ones in MDC documentation (I don't know why they are so un-JavaScript-y over there):

if (!Array.prototype.forEach) {
  Array.prototype.forEach = function(func, scope) {
    for (var i = 0, len = this.length; i < len; i++) {
      func.call(scope, this[i], i, this);
    }
  };
}

But don't use the for ... in construct for arrays - this is meant for objects.

About references

Another thing, sometimes I want to use the element's value and sometimes I want to use it as a reference. What is the syntactical difference?

In JavaScript every variable is in fact a reference to some object. But those references are passed around by value. Let me explain...

You can pass an object to a function that modifies the object and the changes will be seen outside the function:

function incrementHeight(person) {
  person.height = person.height + 1;
}
var p = {height: 10);
alert(p.height); // outputs: 10
incrementHeight(p);
alert(p.height); // outputs: 11

Here you modify the value to which the person reference points to and so the change will be reflected outside the function.

But something like this fails:

function incrementHeight(height) {
  height = height + 1;
}
var h = 10;
alert(h); // outputs: 10
incrementHeight(h);
alert(h); // outputs: 10

Here you create a completely new object 11 and assign its reference to variable height. But variable h outside the function still contains the old reference and so remains to point at 10.

查看更多
【Aperson】
3楼-- · 2020-02-17 09:33

You can use Array.prototype.find, wich does exactly what you want, returns the first element fullfilling the condition. Example:

> ([4, {a:7}, 7, {a:5, k:'r'}, 8]).find(o => o.a == 5)

{a:5, k:'r'}
查看更多
唯我独甜
4楼-- · 2020-02-17 09:35

Just came across the same problem. Tim Down came close, he just needed a wrapper to the length of the filtered array:

// count elements fulfilling a condition
Array.prototype.count = function (f) {
  return this.filter(f).length;  
};

Usage:

// get the answer weight from the question's values array
var w = Math.pow(q.values.count(function(v) { return v !== -1; }), -1);

I hope that answers this long standing question!

查看更多
登录 后发表回答