Javascript object property: string or int?

Bas Peeters

Let's say I have the following object:

var obj {
    name: "Jack", 
    id: 4, 
    year: "2004"
}

I want to iterate through the properties and print out the property type:

for (var i in obj) {
    console.log(i + ' (' + (typeof i) + ') ' + obj[i];
}

The problem is that every type shows as a string:

name: (string) Jack

id: (string) 4

year: (string) 2004

How can I get output the types of "Jack" and "2004" as string and 4 as integer/numeric or something?

putvande

You are outputting the type of your key, not the value. It should be:

for (var prop in obj) {
    console.log(prop + ' (' + (typeof obj[prop]) + ') ' + obj[prop])
}                                     ^^^^^^^^^

That would output:

name (string) Jack
id (number) 4
year (string) 2004

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Get an object property name as string

분류에서Dev

Get Object Property Value in JavaScript

분류에서Dev

javascript, exception for string/object manipulation

분류에서Dev

How to convert number to width string property? Javascript

분류에서Dev

Uncaught InvalidValueError : in property origins : at index 0 : not a string, not a LatLng or LatLngLiteral : not an Object

분류에서Dev

How to remove a property from a JavaScript object when using Strict Mode

분류에서Dev

javascript: use ajax string to create object

분류에서Dev

An object reference is required for the non-static field, method, or property 'TingTong.MainWindow.animategrid(string, string, string)'

분류에서Dev

php array of objects can't get object property (Object of class stdClass could not be converted to string)

분류에서Dev

LINQ를 사용하여 Dictionary <int, List <Tuple <string, object >>>에서 Dictionary <int, List <Tuple <string, object, AnEnum >>>으로

분류에서Dev

String Array를 Object Javascript로 변환

분류에서Dev

Encrypt/decrypt javascript object into/from string - node.js

분류에서Dev

Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

분류에서Dev

How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

분류에서Dev

Converting string to date object, adding two hours and converting back to string (JavaScript)

분류에서Dev

[object property]和object.property有什么区别?

분류에서Dev

Int-> Int, Int-> String, String-> String, String-> Int 일반화

분류에서Dev

String to Int with a + sign

분류에서Dev

String to int array [][] - JAVA

분류에서Dev

String converting to Char and then to Int

분류에서Dev

Turning an attribute into a property on an object-by-object basis?

분류에서Dev

Javascript-Object.property.property를 유효한 속성으로 만들려면 어떻게합니까?

분류에서Dev

Dynamically set object property value

분류에서Dev

Store a reference to a property of an object in a variable

분류에서Dev

Private Property - Object Oriented PHP

분류에서Dev

How to bind control to object property?

분류에서Dev

Determine the datatype of an Object's property

분류에서Dev

PHP: access object with key int

분류에서Dev

Adding OnClick property to array javascript

Related 관련 기사

  1. 1

    Get an object property name as string

  2. 2

    Get Object Property Value in JavaScript

  3. 3

    javascript, exception for string/object manipulation

  4. 4

    How to convert number to width string property? Javascript

  5. 5

    Uncaught InvalidValueError : in property origins : at index 0 : not a string, not a LatLng or LatLngLiteral : not an Object

  6. 6

    How to remove a property from a JavaScript object when using Strict Mode

  7. 7

    javascript: use ajax string to create object

  8. 8

    An object reference is required for the non-static field, method, or property 'TingTong.MainWindow.animategrid(string, string, string)'

  9. 9

    php array of objects can't get object property (Object of class stdClass could not be converted to string)

  10. 10

    LINQ를 사용하여 Dictionary <int, List <Tuple <string, object >>>에서 Dictionary <int, List <Tuple <string, object, AnEnum >>>으로

  11. 11

    String Array를 Object Javascript로 변환

  12. 12

    Encrypt/decrypt javascript object into/from string - node.js

  13. 13

    Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

  14. 14

    How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

  15. 15

    Converting string to date object, adding two hours and converting back to string (JavaScript)

  16. 16

    [object property]和object.property有什么区别?

  17. 17

    Int-> Int, Int-> String, String-> String, String-> Int 일반화

  18. 18

    String to Int with a + sign

  19. 19

    String to int array [][] - JAVA

  20. 20

    String converting to Char and then to Int

  21. 21

    Turning an attribute into a property on an object-by-object basis?

  22. 22

    Javascript-Object.property.property를 유효한 속성으로 만들려면 어떻게합니까?

  23. 23

    Dynamically set object property value

  24. 24

    Store a reference to a property of an object in a variable

  25. 25

    Private Property - Object Oriented PHP

  26. 26

    How to bind control to object property?

  27. 27

    Determine the datatype of an Object's property

  28. 28

    PHP: access object with key int

  29. 29

    Adding OnClick property to array javascript

뜨겁다태그

보관