如何获取数组的第一个元素? [英] How to get the first element of an array?

查看:98
本文介绍了如何获取数组的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从这样的数组中获取第一个元素:

var ary = ['first', 'second', 'third', 'fourth', 'fifth'];

我试过了:

alert($(ary).first());

但它会返回[object Object].所以我需要从数组中获取第一个元素,它应该是元素 'first'.

解决方案

你为什么要用 jQuery 化一个普通的 JavaScript 数组?使用标准 JavaScript!

var ary = ['first', 'second', 'third', 'fourth', 'fifth'];警报(ary [0]);

来源,由 bobince

How do you get the first element from an array like this:

var ary = ['first', 'second', 'third', 'fourth', 'fifth'];

I tried this:

alert($(ary).first());

But it would return [object Object]. So I need to get the first element from the array which should be the element 'first'.

解决方案

Why are you jQuery-ifying a vanilla JavaScript array? Use standard JavaScript!

var ary = ['first', 'second', 'third', 'fourth', 'fifth'];
alert(ary[0]);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Also,

Source, courtesy of bobince

这篇关于如何获取数组的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆