[英]Checking the type of an element in an array in Javascript
Just started with Javascript and am trying to check the type of an element in an array
: I have a function
that takes two functions as parameters
. 刚从Javascript开始,正在尝试检查array
元素的类型:我有一个function
,需要两个函数作为parameters
。 I have an array
which has a combination of string
and number
elements. 我有一个array
string
和number
元素组合的array
。 The idea is to trigger functions
based on the type of element in the sample array
and then push a new element
to a new array
. 这个想法是根据样本array
中元素的类型触发functions
,然后将新element
推到新array
。 I'm not sure which bulit in function i can use here. 我不确定我可以在这里使用哪个功能。 I tried indexOf
and typeof
but these don't seem to serve the purpose or maybe I'm doing this wrong. 我尝试了indexOf
和typeof
但是这些似乎没有达到目的,或者我做错了。 Below is the code. 下面是代码。 Thanks a lot! 非常感谢!
var arr1 = [3, "Hello", 5, "Hola"];
function setNewArray(arr, funct1, funct2){
var arr2 = [];
for(var i = 0; i < arr.length; i++){
if(/*check if arr[i] is a string*/){
arr2.push(funct1(arr[i]));
}
if(/*check if arr[i] is a number*/){
arr2.push(funct2(arr[i]));
}
}
return arr2;
}
var numfunct = function(item){
return item * 2;
}
var strfunct = function(item){
return item + " " + "there";
}
var result = setNewArray(arr1, numfunct, strfunct);
console.log(result);
You can do it easily using typeof
like following 您可以使用typeof
轻松完成以下操作
For the first code block 对于第一个代码块
if(typeof arr[i] === 'string'){
arr2.push(
funct1(arr[i]));
}
For the second one 对于第二个
if(typeof arr[i] === 'number'){
arr2.push(
funct2(arr[i]));
}
Check this: 检查一下:
var arr1 = [3, "Hello", 5, "Hola" ]; function setNewArray(arr, funct1, funct2){ var arr2 = []; for(var i = 0; i < arr.length; i++){ if(!isNaN(arr[i])){ // or use this alternative typeof arr[i] == 'number' arr2.push( funct1(arr[i])); } if(typeof (arr[i]) == 'string'){ arr2.push( funct2(arr[i])); } } return arr2; } var numfunct = function(item){ return item * 2; } var strfunct = function(item){ return item + " " + "there"; } var result = setNewArray(arr1, numfunct, strfunct); console.log(result);
Create an object of functions, where each function name is the type it can handle. 创建一个函数对象,其中每个函数名称都是它可以处理的类型。 Iterate the array with Array#map , and select the right method from the object using typeof . 使用Array#map迭代数组,然后使用typeof从对象中选择正确的方法。
Note: I've added a boolean handler to convert the true
to yes. 注意:我添加了一个布尔处理程序,将true
转换为yes。
var arr1 = [3, "Hello", 5, "Hola", true]; var fns = { number(item) { return item * 2; }, string(item) { return item + " " + "there"; }, boolean(item) { return item ? 'yes' : 'no' } }; function setNewArray(arr, fns) { return arr.map(function(item) { return fns[typeof item](item); }); } var result = setNewArray(arr1, fns); console.log(result);
You can use typeof
to check the type of any variable or object. 您可以使用typeof
检查任何变量或对象的类型。 And you do not need to pass the function refrence. 而且您不需要传递函数引用。 Here is the code: 这是代码:
var arr1 = [3, "Hello", 5, "Hola"];
function setNewArray(arr) {
var arr2 = [];
for (var i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'string') {
arr2.push(
strfunct(arr[i]));
}
if (typeof arr[i] === 'number') {
arr2.push(
numfunct(arr[i]));
}
}
return arr2;
}
var numfunct = function (item) {
return item * 2;
}
var strfunct = function (item) {
return item + " " + "there";
}
var result = setNewArray(arr1);
console.log(result);
Try using typeof Operator in javascript 尝试在JavaScript中使用typeof运算符
var arr1 = [3, "Hello", 5, "Hola"]; function setNewArray(arr, funct1, funct2) { var arr2 = []; for (var i = 0; i < arr.length; i++) { if (typeof arr[i] == 'string') { arr2.push(funct1(arr[i])); } if (typeof arr[i] == 'number') { arr2.push(funct2(arr[i])); } } return arr2; } var numfunct = function (item) { return item * 2; } var strfunct = function (item) { return item + " " + "there"; } var result = setNewArray(arr1, strfunct, numfunct); console.log(result);
I think this is the exact function that you are looking for. 我认为这是您正在寻找的确切功能。
The problem was not with the use of typeof operator but with the function called with number and string. 问题不在于使用typeof运算符,而在于用数字和字符串调用的函数。
Updated code: 更新的代码:
var arr1 = [3, "Hello", 5, "Hola" ];
function setNewArray(arr, funct1, funct2){
var arr2 = [];
for(var i = 0; i < arr.length; i++){
if(typeof arr[i] == "string"){
arr2.push(
funct2(arr[i]));
}
if(typeof arr[i] == "number"){
arr2.push(
funct1(arr[i]));
}
}
return arr2;
}
var numfunct = function(item){
return item * 2;
}
var strfunct = function(item){
return item + " " + "there";
}
var result = setNewArray(arr1, numfunct, strfunct);
console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.