简体   繁体   English

如何在不使用 method.length 的情况下计算数组中元素的数量

[英]How to count number of elements in array without using method .length

function addValue(arr, value){ function 添加值(arr,值){

arr.push(value) arr.push(值)

return...返回...

} }

console.log(addValue([1, 2, 3], 10)); console.log(addValue([1, 2, 3], 10)); // 4 without using.length // 4 不使用.length

Array#push returns the new length of the array. Array#push返回数组的新长度。

 function addValue(arr, value) { return arr.push(value); } console.log(addValue([1, 2, 3], 10)); // 4 without using.length

Try this pure javascript return length of the array without.length试试这个纯 javascript 返回数组的长度 without.length

var a = [1,2,3,4,5];

function arrayLength(a){
  var length = 0;
  while(a[length]!==undefined){
    length++;
  }
  return length;
}

console.log(arrayLength(a));       // 5
function addValue(arr, value){

arr.push(value)
var count=0;
arr.forEach(a=>{
count++;})
return count

}

console.log(addValue([1, 2, 3], 10)); 

If you don't want to use.length, then you can make your own length variable by manually counting them如果你不想使用.length,那么你可以通过手动计算它们来制作你自己的长度变量

function addValue(arr, value){

    arr.push(value)

    return ( ()=> {
         var mylength=0
         for(var k in a) mylength+=1
         return mylength
    } )()

}

Although I'm guessing the reason you don't want to use.length is simply because your using some kind of sudo-array that doesn't have a length property, in which case you can just convert it to an array虽然我猜你不想使用的原因。长度仅仅是因为你使用了某种没有长度属性的sudo数组,在这种情况下你可以将它转换为数组

....
return Array.apply(null, ar).length

If not though length is pretty much what you want如果不是,虽然长度几乎就是你想要的

Another option, again, as shorthand for counting the elements, is to just the length of a list of the keys同样,作为计算元素的简写方式,另一种选择是只计算键列表的长度

return Object.keys(ar).length

Or Object.values或者 Object.values

return Object.values(ar).length

Or Object.entries或 Object.entries

return Object.entries(ar).length

(Usually these methods are good for sudo-array or custom arrays made from scratch that don't have a length property) (通常这些方法适用于 sudo-array 或从头开始制作的自定义 arrays 没有长度属性)

Or just return the push function或者只返回推送 function

return ar.push(value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Javascript计算数组中的元素数 - Count number of elements in an array using Javascript 计算数组长度的简写方法 - Shorthand method to count length of array 如何计算给定数字以下/上方的数组中的元素数(javascript) - How to count the number of elements in an array below/above a given number (javascript) 如何使用javascript仅按名称计算元素长度而不显示无? - How to count elements length by name only not display none using javascript? 如何在不使用本机长度方法的情况下在 javascript 中获取字符串的长度 - How to get length of string in javascript without using native length method 在javascript中计算数组的元素数 - Count the number of elements of an array in javascript 如何使用 object 属性计算对象数组中唯一元素的数量? - How do I count the number of unique elements in an array of objects using the object property? 使用对象数组,如何计算 active 属性为 true 的元素数量? - With an array of objects, how to count the number elements with active property true? MongoDB 如何使用文档数组中的元素数更新计数字段 - MongoDB how to update a count field with the number of elements that are in an array on the document 如何计算使用数组检查的复选框的数量 - How to count the number of checkboxes checked using an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM