/ 设置 10 秒超时 // 每日统计清 0 runtime_set('todaycomments', 0); runtime_set('todayarticles', 0); runtime_set('todayusers', 0); if ($forumlist) { $fidarr = array(); foreach ($forumlist as $fid => $forum) { $fidarr[] = $forum['fid']; } forum_update($fidarr, array('todayposts' => 0, 'todaythreads' => 0)); } // 清理临时附件 attach_gc(); // 当天24点 $today = strtotime(date('Ymd')) + 86400; runtime_set('cron_2_last_date', $today, TRUE); // 往前推8个小时,尽量保证在前一天 升级过来和采集的数据会很卡 // table_day_cron($time - 8 * 3600); cache_delete('cron_lock_2'); } } } ?>Javascript, Getting Length of String Held in Array - Stack Overflow|Concepts Of Algorithm

Javascript, Getting Length of String Held in Array - Stack Overflow

admin2025-03-03  34

k = [['a'], ['ab'], ['abc']];
alert(k[2].length);

The above code fragment returns 1.

How can I get the length of the string, 3 in this case?

k = [['a'], ['ab'], ['abc']];
alert(k[2].length);

The above code fragment returns 1.

How can I get the length of the string, 3 in this case?

Share Improve this question asked Aug 14, 2012 at 21:56 user1541722user1541722 391 gold badge2 silver badges4 bronze badges 2
  • 2 You have an array of arrays... is that really what you want? Why don't you just do console.log(k[2]) and see what k[2] is? – Felix Kling Commented Aug 14, 2012 at 21:58
  • Maybe a reading a Javascript Array Tutorial will help. – BryanH Commented Aug 15, 2012 at 5:43
Add a ment  | 

2 Answers 2

Reset to default 9

The object is not what is expected. Consider:

k = [['a'], ['ab'], ['abc']];
a = k[2]    // -> ['abc']
a.length    // -> 1 (length of array)
b = k[2][0] // -> 'abc'
b.length    // -> 3 (length of string)

In your example, k is not a normal array containing strings. It contains sub-arrays, which contain the strings. You should declare k this way:

k = ['a', 'ab', 'abc'];

If you do want to use your own declaration, you could do this:

alert(k[2][0].length);​
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1740975457a137281.html

最新回复(0)