You might think that to check if an object is an array, we can simply use the typeof operator.
您可能会认为,要检查对象是否为数组 ,我们可以简单地使用typeof运算符 。
const var1 = 10;
const str = 'hello'
console.log(typeof var1);
console.log(typeof str);
Output
输出量
"number"
"string"
Well the typeof operator works fine for numbers and strings. What about an object?
那么typeof运算符可以很好地处理数字和字符串。 那物体呢?
const obj= {
name: 'Lily', age: 18
}
console.log(typeof obj);
Output
输出量
object
So it works for objects too. Let's try it out for an array,
因此它也适用于对象。 让我们尝试一个数组,
const arr = [1, 2, 3, 4, 5, 6];
console.log(typeof arr);
Output
输出量
object
The typeof operator tells the type of object that data set is and not it's inherent type. Arrays are a derived data type of objects hence they are considered as a plain simple objects. We can check using a method isArray() to see if our object is an array.
typeof运算符告诉数据集是对象的类型,而不是它的固有类型。 数组是对象的派生数据类型,因此它们被视为普通的简单对象。 我们可以使用isArray()方法检查我们的对象是否为array 。
Let's say we have the following object,
假设我们有以下对象,
const obj={
name: 'Courage',
animal: 'Dog',
color: 'Pink',
show: 'Courage the cowardly dog',
genre: 'Horror-comedy'
}
console.log(Array.isArray(obj));
Output
输出量
false
The way we use isArray() is that we always call it on the Array object. This method was introduced in ES6 and it's built on top of the Array class. Inside the method, we pass in the data set, object or array whatever we want to be evaluated as a parameter. It returns a boolean value, true if the parameter is an array and false otherwise.
我们使用isArray()的方式是,我们总是在Array对象上调用它。 此方法是在ES6中引入的,它建立在Array类的顶部。 在方法内部,我们传入要设置为参数的数据集,对象或数组。 它返回一个布尔值,如果参数是数组,则返回true,否则返回false。
Let's check it out for a few more examples,
让我们看看更多示例,
const colors=['red','orange','blue',{
date:'12th December',
day:'Thursday',
year: 2019
}
]
console.log(Array.isArray(colors));
Output
输出量
true
Indeed colors is an array and not an object. It contains an object as one of the values inside it, but it's still very much an array.
的确,颜色是数组而不是对象 。 它包含一个对象作为其中的值之一,但是它仍然是一个数组。
const data = {
colors: ['blue', 'green', 'red'],
names: ['Sarah', 'Remi', 'Roy']
}
console.log(Array.isArray(data))
console.log(Array.isArray(data.colors));
console.log(Array.isArray(data.names));
Output
输出量
false
true
true
Data is an object containing the arrays: names and colors. Hence we get a false for data and true for the arrays colors and names. It's quite clear that this method is built on the syntactical definition of arrays. The square brackets and curly brackets distinguish an object to be an array or pure object respectively. Can you implement your isArray() method? It's quite difficult to do so looking at the way it's built on syntax, at least so it seems. What if you convert it to a string and look for the first bracket that comes in the string? Think, you might come to an interesting way to implement your isArray() method!
数据是一个包含数组的对象: 名称和颜色 。 因此,对于数据,我们得到的是错误的,对于数组颜色和名称,则得到的是true。 很明显,此方法建立在数组的句法定义上。 方括号和大括号分别将对象区分为数组或纯对象。 您可以实现isArray()方法吗? 至少从表面上看,这样做很难看待它基于语法的构建方式。 如果将其转换为字符串并寻找字符串中的第一个括号怎么办? 想想,您可能会想到一种有趣的方法来实现isArray()方法 !
翻译自: https://www.includehelp.com/code-snippets/how-to-check-object-is-an-array-in-javascript.aspx