如何检查对象是JavaScript中的数组?

news/2024/7/19 15:06:32 标签: python, java, js, javascript, vue

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


http://www.niftyadmin.cn/n/1255752.html

相关文章

2018 CISCN reverse

2018 CISCN reverse 这题比赛的时候没做出来,主要是心态崩了看不下去。。赛后看了下网上的wp发现不难,是自己想复杂了。这里将我的思路和exp放出来,希望大家一起交流学习。 main函数 它首先是check了输入的前六个字符是否与“CISCN{”匹配&am…

javascript运算符_使用JavaScript中的示例删除运算符

javascript运算符JavaScript删除运算符 (JavaScript delete Operator) "delete" is an operator in JavaScript and it is used to delete a property of an object. After deleting the particular property, that property will not be accessible and returns &qu…

Python进阶(二)

高阶函数 1.把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式。 2.Python内建了map( )和reduce( ) 函数 map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用…

as_hash ruby_Hash.merge!(other_hash)方法与Ruby中的示例

as_hash rubyHash.merge!(other_hash)方法 (Hash.merge!(other_hash) Method) In this article, we will study about Hash.merge!(other_hash) Method. The working of the method can’t be assumed because it’s quite a different name. Let us read its defin…

2018 计蒜之道 初赛 第二场

签到完看到C没什么人过就溜乐。 A.淘宝的推荐系统 直接DP&#xff0c;时间复杂度$O(∑nd)$ #include <bits/stdc.h>using namespace std;#define rep(i, a, b) for (int i(a); i < (b); i) #define dec(i, a, b) for (int i(a); i > (b); --i) #define MP make_pa…

在Python中创建目录并处理异常

Python os.mkdir()方法 (Python os.mkdir() method) The built-in, os module is useful in creating directories. The syntax to create the directory is, 内置的“ os”模块可用于创建目录 。 创建目录的语法为 &#xff0c; os.mkdir(<path>)Python code to create…

类似jQuery的原生JS封装的ajax方法

一&#xff0c;前言&#xff1a; 前文&#xff0c;我们介绍了ajax的原理和核心内容&#xff0c;主要讲的是ajax从前端到后端的数据传递的整个过程。 Ajax工作原理和原生JS的ajax封装 真正的核心就是这段代码&#xff1a; var xhr new XMLHTTPRequest(); xhr.open("method…

Python线程模块| setprofile()方法与示例

Python threading.setprofile()方法 (Python threading.setprofile() Method) setprofile() is an inbuilt method of the threading module in Python. It is used to set a profile function for all the threads that are created by the threading module. The func functi…