我有两个数组,我想将第一个数组的值添加到第二个数组。我是通过.push
实现的。
数组初始化:
<script>
var needsPrecision = ["Decimal"];
var needsLength = ["Binary", "Char", "NChar", "NVarChar", "VarBinary", "VarChar"];
needsLength.push(needsPrecision);
此后,我遍历了它们,并且"Decimal"
在needsLength
数组中:
for (var i = 0; i < needsLength.length; i++) {
alert (needsLength[i]);
}
alert("---");
for (var i = 0; i <needsPrecision.length; i++) {
alert(needsPrecision[i]);
}
但是,如果我在$(document).ready()
中对其进行检查,它将返回-1
-> false
:
$(document).ready(function () {
alert($.inArray("Decimal", needsLength));
}
</script>
我错过了一些示波器吗? (对不起,我对jquery还是很陌生的)
答案 0 :(得分:1)
needsPrecision
是一个数组。您需要Array.concat像下面这样的数组。
var needsPrecision = ["Decimal"];
var needsLength = ["Binary", "Char", "NChar", "NVarChar", "VarBinary", "VarChar"];
needsLength = needsLength.concat(needsPrecision);
答案 1 :(得分:0)
您还可以使用扩展语法组合两个数组,然后获取索引:
var needsPrecision = ["Decimal"];
var needsLength = ["Binary", "Char", "NChar", "NVarChar", "VarBinary"];
needsLength = [...needsLength, ...needsPrecision];
console.log(needsLength);
alert($.inArray("Decimal", needsLength));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您甚至可以使用Array.push.apply()
组合数组,然后获取索引:
var needsPrecision = ["Decimal"];
var needsLength = ["Binary", "Char", "NChar", "NVarChar", "VarBinary"];
needsLength.push.apply(needsLength, needsPrecision);
console.log(needsLength);
alert($.inArray("Decimal", needsLength));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>