JavaScript - get array element fulfilling a condition

后端 未结 9 882
感动是毒
感动是毒 2020-12-08 06:18

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 conditi

9条回答
  •  [愿得一人]
    2020-12-08 07:02

    You can use for ... in in JavaScript:

    for (var key in array) {
        if (/* some condition */) {
            // ...
        }
    }
    

    As of JavaScript 1.6, you can use this, too:

    for each (var element in array) {
        // ...
    }
    

    These are mainly meant to traverse object properties. You should consider to simply use your for-loop.

    EDIT: You could use a JavaScript framework like jQuery to eliminate these cross-browser problems. Give it a try. Its $.each()-method does the job.

提交回复
热议问题