te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>jquery - Array value count javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Array value count javascript - Stack Overflow

programmeradmin3浏览0评论

How can I count the array depending on the value.. I have an array with this value..

var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";

I need to count the array depending on the value

Array with Value A is 2 Array with value B is 1

Thanks!

How can I count the array depending on the value.. I have an array with this value..

var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";

I need to count the array depending on the value

Array with Value A is 2 Array with value B is 1

Thanks!

Share Improve this question asked Dec 22, 2011 at 17:12 user1033600user1033600 2894 gold badges8 silver badges17 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 5
var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";


function count(array, value) {
  var counter = 0;
  for(var i=0;i<array.length;i++) {
    if (array[i] === value) counter++;
  }
  return counter;
}

var result = count(myArr, "a");
alert(result);

If you're interested in a built-in function... you could always add your own.

Array.prototype.count = function(value) {
  var counter = 0;
  for(var i=0;i<this.length;i++) {
    if (this[i] === value) counter++;
  }
  return counter;
};

Then you could use it like this.

var result = myArr.count("a");
alert(result);

Oh well, beaten to the punch, but here's my version.

var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b"

getCountFromVal( 'a', myArr );

function getCountFromVal( val, arr )
{
    var total =  arr.length;
    var matches = 0;

    for( var i = 0; i < total; i++ )
    {
        if( arr[i] === val )
        {
            matches++;
        }
    }

    console.log(matches);
    return matches;
}

Live Demo: http://jsfiddle/CLjyj/1/

var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";

function getNum(aVal)
{
    num=0;
    for(i=0;i<myArr.length;i++)
    {
        if(myArr[i]==aVal)
            num++;
    }
    return num;
}

alert(getNum('a')); //here is the use

I would use Array.filter

  var arr = ['a', 'b', 'b']
  arr.filter(val => val === 'b').length // 2

Everyone's already given the obvious function, so I'm going to try and make another solution:

var myArr = [];
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";

function count(arr, value){
    var tempArr = arr.join('').split(''), //Creates a copy instead of a reference
        matches = 0,
        foundIndex = tempArr.indexOf(value); //Find the index of the value's first occurence
    while(foundIndex !== -1){
        tempArr.splice(foundIndex, 1); //Remove that value so you can find the next
        foundIndex = tempArr.indexOf(value);
        matches++;
    }
    return matches;
}

//Call it this way
document.write(count(myArr, 'b'));

Demo

You can use Lodash's countBy function:

_.countBy(myArr);

Example:

var myArr = new Array(3);
myArr[0] = "a";
myArr[1] = "a";
myArr[2] = "b";

const result = _.countBy(myArr);

// Get the count of `a` value
console.log('a', result.a);

// Get the count of `b` value
console.log('b', result.b);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.15/lodash.js"></script>

countBy Documentation

发布评论

评论列表(0)

  1. 暂无评论