我有一个有一些数据(数组)的函数,我希望它总是返回一个值,它可以是字符串或一个元素数组。不知道该怎么做。代码示例如下。你能救我吗?
function somefunction() {
var data = ['first', 'second', 'third'],
arr = [];
$.each(data, function (index, value) {
arr.push(value);
console.log(value)
});
return arr;
}
somefunction()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我认为它应该有用,但它没有
function somefunction() {
var data = ['first', 'second', 'third'],
arr = [],
i;
$.each(data, function (index, value) {
arr.push(value);
i++;
console.log(value)
});
return arr[i];
}
somefunction()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>