/* TASK: create a function that merges an array of arrays into one, and then
pushes the strings into the str array and push the numbers into the num array.
The values are not being pushed into the str or nums array even though the typeof operator is working in the if statement. Why? */
let ans = (arr) => {
let str = [];
let nums = [];
let inOneArr = arr.reduce((a,b)=>{
return a.concat(b);
})
alert(inOneArr);
for(let i = 0; i < inOneArr.length; i++) {
if(inOneArr[i] == typeof(string)) {
str.push(inOneArr[i]);
} else if (inOneArr[i] == typeof(number)) {
nums.push(inOneArr[i]);
}
}
alert(str);
alert(nums);
}
ans([["strings","bolls","netflix",1,4,5],["felix",22,"@gmail.com"],[32,"@#@@!","Chris"]])
&#13;
答案 0 :(得分:3)
typeof是一个关键词,它不需要括号,并返回该类型的字符串;例如"string"
或"boolean"
。
它应该用在你想要找到的类型的元素上。不喜欢:typeof(number)
。
function ans() {
let str = [];
let nums = [];
let inOneArr = arr.reduce((a, b) => {
return a.concat(b);
})
alert(inOneArr);
for (let i = 0; i < inOneArr.length; i++) {
if (typeof inOneArr[i] === "string") {
str.push(inOneArr[i]);
} else if (typeof inOneArr[i] === "number") {
nums.push(inOneArr[i]);
}
alert(str);
alert(nums);
}
}
ans([
["strings", "bolls", "netflix", 1, 4, 5],
["felix", 22, "@gmail.com"],
[32, "@#@@!", "Chris"]
]);
答案 1 :(得分:0)
typeof的语法是:
df <- read.table(text = "a b c
1: 1 1 1
2: 1 2 1
3: 1 2 2
4: 2 1 1
5: 2 2 5
6: 2 3 3
7: 3 1 4
8: 3 2 1", header = TRUE, stringsAsFactors = FALSE)
typeof运算符返回一个字符串,指示未评估的操作数的类型。
+----- This is the operand to be evaluated
|
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
typeof [(object | value returned from a function, Etc)].
运算符后跟其操作数:
typeof
typeof operand
or
typeof (operand)
是一个表达式,表示要返回其类型的对象或基元。
operand