Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- input
- 태그
- mybatis
- eGov
- 함수
- spring
- was
- controller
- TO_DATE
- 과정평가형
- JVM
- 개념
- CSS
- Oracle
- 정의
- 암호화
- eGovFramework
- Ajax
- jsp
- 오류
- Java
- javascript
- jQuery
- POI
- array
- select
- html
- json
- sql
- web.xml
Archives
- Today
- Total
web developer
[javaScript] When to Use Arrays. When to use Objects. 본문
728x90
728x90
When to Use Arrays. When to use Objects.
- JavaScript does not support associative arrays.
- You should use objects when you want the element names to be strings (text).
- You should use arrays when you want the element names to be numbers.
개체 속성에 접근할 수 있는 방법 2가지
1) objectName.propertyName
2) objectName["propertyName"]
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p>If you use a named index when accessing an array, JavaScript will redefine the array to a standard object, and some array methods and properties will produce undefined or incorrect results.</p>
<p id="demo"></p>
<script>
// 1번 (정상) - array
const person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
// 2번 (오류) - err
const person = [];
person["firstName"] = "John";
person["lastName"] = "Doe";
person["age"] = 46;
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
// 3번 (정상) - let
let firstName = 0;
let lastName = 1;
let age = 2;
const person = [];
person[firstName] = "John";
person[lastName] = "Doe";
person[age] = 46;
// 4번 (정상) - object
let df = {
firstName : 0
,lastName : 1
,age : 2
}
const person = [];
person[df.firstName] = "John";
person[df.lastName] = "Doe";
person[df.age] = 46;
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
</script>
</body>
</html>
출처 : https://www.w3schools.com/js/js_arrays.asp
JavaScript Arrays
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
728x90
728x90
'JavaScript' 카테고리의 다른 글
[jquery] .text() 메소드, .html() / 기존 요소의 내부에 새로운 요소나 콘텐츠를 반환하거나 설정하기 (0) | 2022.01.21 |
---|---|
[jstl] EL(Expression Language) 구문 (0) | 2022.01.20 |
[ajax] ajax 정의, $.ajax() 메소드, 데이터 형식 (0) | 2021.12.24 |
[jquery] DOM(문서 객체 모델) / jquery 문법 / 선택자 (0) | 2021.12.13 |
[jquery] CSS 선택자를 이용하여 HTML 요소 선택하는 방법 4가지 (0) | 2021.10.24 |