最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript - How can I fetch a non zero value from an array? - Stack Overflow

programmeradmin0浏览0评论

I have an array that will have just a single non zero value along with the other 0 values at a time. For example, it may be

[0,0,0,0,0,0,0,1234,0,0,0] 
// or
[0,0,2823,0,0,0,0,0,0,0,0]
//...

My question is, how may I get this non zero value from the array using javascript.

I know that I can iterate over the array and return the non zero value as soon as it is found but I was wondering if there is any functionality provided by javascript to do this?

I have an array that will have just a single non zero value along with the other 0 values at a time. For example, it may be

[0,0,0,0,0,0,0,1234,0,0,0] 
// or
[0,0,2823,0,0,0,0,0,0,0,0]
//...

My question is, how may I get this non zero value from the array using javascript.

I know that I can iterate over the array and return the non zero value as soon as it is found but I was wondering if there is any functionality provided by javascript to do this?

Share Improve this question asked Dec 23, 2013 at 13:52 Kamran AhmedKamran Ahmed 12.4k23 gold badges72 silver badges103 bronze badges 5
  • 3 @VisioN gave a good answer, but it should be noted that something is going to iterate through the array. The functional approach in that answer is nice in that it "covers up" the iteration, but algorithmically it's still a real cost. – Pointy Commented Dec 23, 2013 at 13:54
  • @Pointy depends. Can the functional approach be vectorised better than the iterative approach? (I think it's the other way around, and iterative will be faster). – John Dvorak Commented Dec 23, 2013 at 13:55
  • @JanDvorak well perhaps, but if you're doing traditional plexity analysis looking up a value in an unordered linear list is an O(n) operation. – Pointy Commented Dec 23, 2013 at 13:57
  • @Pointy correct, but sixteen-per-clock-cycle type of O(N) is still much better than one-per-sixteen-cycles type of O(N)) :-) – John Dvorak Commented Dec 23, 2013 at 13:58
  • Unlike, for example, a C# SortedList, arrays have always to be iterated via brute-force for parison. Hence, the speediest way is to go from one end through to the other. – Bora Commented Dec 23, 2013 at 13:59
Add a ment  | 

2 Answers 2

Reset to default 7

You may filter it out from the array:

[0,0,0,0,0,0,0,1234,0,0,0].filter(function(x) { return x; }).pop();  // 1234

As @Sarath mentioned in the ments, if your initial array may have other falsy non numeric values (as false, undefined, '', etc) you may add a strict parison:

[0,0,0,0,0,0,0,1234,0,0,0].filter(function(x) { return x !== 0; }).pop();

Another short solution for numeric arrays is using reduce method:

[0,0,0,0,0,0,0,1234,0,0,0].reduce(function(a, b) { return a + b; }); // 1234

N.B.: Check the browser patibility for filter and reduce methods and use polyfills if required.

Just thought I'd offer an alternate solution given the constraints of your question:

var foo = [0,0,0,0,0,0,0,1234,0,0,0],
    bar = [0,0,2823,0,0,0,0,0,0,0,0];

console.log(
    Math.max.apply(null, foo), // 1234
    Math.max.apply(null, bar)  // 2823
);
发布评论

评论列表(0)

  1. 暂无评论