如何检查一个Javascript对象是否包含一个数组作为属性的值?

时间:2021-01-04 07:30:30

I have an object coming from the form data (in javascript using express handlebars), which can be as one of the following:

我有一个对象来自于表单数据(用javascript使用express handlebar),它可以作为以下内容之一:

object1 = { name: "Grade1", section: "A", courses: [ "Eng", "Math", "Sci"] }

OR

object2 = { name: "Grade1", section: "A", courses: "Sci" }

How can I determine whether the courses attribute contains an array or a single value? Or is there a way to send the data from the form as an array for the attribute courses always (even if it is a single course)?

我如何确定课程属性包含数组还是单一值?或者是否有一种方法可以将表单中的数据作为属性课程的数组(即使是单一的课程)?

Thanks.

谢谢。

3 个解决方案

#1


2  

You can also use the isArray method. Note the documentation which explains why you may want to use this method as opposed to instanceOf:

您还可以使用isArray方法。请注意说明为什么要使用这种方法而不是instanceOf的文档:

When checking for Array instance, Array.isArray is preferred over instanceof because it works through iframes.

当检查数组实例时,数组。isArray优先于instanceof,因为它通过iframe工作。

You may prefer this depending on your use case (assuming you're working with a browser) and gives you an opportunity to send it as an array if it isn't one already. Something like:

您可能更喜欢这取决于您的用例(假设您使用的是浏览器),并给您一个机会将它作为一个数组发送,如果它还不是一个数组的话。喜欢的东西:

var object1 = { name: "Grade1", section: "A", courses: [ "Eng", "Math", "Sci"] };
var object2 = { name: "Grade1", section: "A", courses: "Sci" };

console.log(object2.courses);

if (!Array.isArray(object2.courses)) {
    object2.courses = [object2.courses];
 }

 console.log(object2.courses);

#2


3  

You could use the instanceof or typeof operator:

您可以使用instanceof或typeof操作符:

To check if its an array:

检查它是否是一个数组:

if (object.courses instanceof Array) {
  ···
}

To check if its a string:

检查它是否是一个字符串:

if (typeof object.courses === 'string') {
  ···
}

#3


-1  

You can use typeof to check if object1.courses is an array or a string.

您可以使用typeof检查object1。课程是数组或字符串。

There are ways of making sure courses is always an array when you receive the data.

有一些方法可以确保在接收数据时,课程始终是一个数组。

#1


2  

You can also use the isArray method. Note the documentation which explains why you may want to use this method as opposed to instanceOf:

您还可以使用isArray方法。请注意说明为什么要使用这种方法而不是instanceOf的文档:

When checking for Array instance, Array.isArray is preferred over instanceof because it works through iframes.

当检查数组实例时,数组。isArray优先于instanceof,因为它通过iframe工作。

You may prefer this depending on your use case (assuming you're working with a browser) and gives you an opportunity to send it as an array if it isn't one already. Something like:

您可能更喜欢这取决于您的用例(假设您使用的是浏览器),并给您一个机会将它作为一个数组发送,如果它还不是一个数组的话。喜欢的东西:

var object1 = { name: "Grade1", section: "A", courses: [ "Eng", "Math", "Sci"] };
var object2 = { name: "Grade1", section: "A", courses: "Sci" };

console.log(object2.courses);

if (!Array.isArray(object2.courses)) {
    object2.courses = [object2.courses];
 }

 console.log(object2.courses);

#2


3  

You could use the instanceof or typeof operator:

您可以使用instanceof或typeof操作符:

To check if its an array:

检查它是否是一个数组:

if (object.courses instanceof Array) {
  ···
}

To check if its a string:

检查它是否是一个字符串:

if (typeof object.courses === 'string') {
  ···
}

#3


-1  

You can use typeof to check if object1.courses is an array or a string.

您可以使用typeof检查object1。课程是数组或字符串。

There are ways of making sure courses is always an array when you receive the data.

有一些方法可以确保在接收数据时,课程始终是一个数组。