오도원입니다.

건강과 행복을 위하여

반응형

Development 71

Javascript. JSON 데이터 추가(add) / 삭제(delete)

An object is an unordered set of name/value pairs. JSON 데이터를 추가, 삭제하는 방법 1. 추가 obj.key_name = value; 2. 삭제 delete obj.key_name; 3. 예시 let person = { name : "오도원", age : 24, job : 'student' }; console.log('>>>>> person'); console.log(person); console.log(">>>>> add gender"); person.gender = "male"; console.log(person); console.log(">>>>> delete job"); delete person.job console.log(person);

Javascript. 진짜로 Deep Copy하기

Javascript에서 Array 또는 Object를 복사하는 방법에 대해서 알아보자. 일단 결론부터 말하면 다음과 같이 하면된다. let deepArr = JSON.parse(JSON.stringify(arr1)); 다음은 자세한 설명이다. 1. 깊은 복사(Deep Copy)와 얕은 복사(Shallow Copy) 얕은 복사는 alias로 인해 같은 주소를 공유하기 때문에 복사한 객체를 변경하면 기존 객체도 같이 변경되는 문제가 발생할 수 있다. 따라서 복사를 할 때는 깊은 복사를 해야 추후 예상치 못한 문제를 방지할 수 있다. 2. 자바스크립트에서의 복사 1) 얕은 복사 - 대입 let arr1 = [ {"name": "오도원", "job": "player"}, {"name": "오도투", "job":..

Javascript. sum of array, 배열의 원소 합구하기, reduce 함수이용

reduce함수를 이용한다. let arr = [1, 2, 3, 4]; let ret = arr.reduce((a, b) => a + b, 0); console.log(ret); Array.prototype.reduce() arr.reduce(callback[, initialValue]) callback 함수 구조. const callback = (accumulator, currentValue) => accumulator + currentValue; accumulator에 currentValue값이 누적된다. 참고. 1. reduce를 활용한 array sum https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-nu..

Javascript. JSON to String OR String to JSON; JSON을 string으로 변환; string을 JSON으로 변환

JSON 데이터의 타입은 Object이다. 1. JSON to String JSON을 string으로 바꿀 때는 JSON.stringify() 함수를 사용한다. jData = {"stage1":100, "stage2":100, "stage3":100}; sData = JSON.stringify(jData); console.log(`typeof jData = ${typeof jData}\njson=>`,jData, '\n'); console.log(`typeof sData = ${typeof sData}\nstring=>${sData}`); 2. String to JSON mysql, mariadb과 같은 db에서 json은 longtext타입으로 저장하기 때문에 데이터를 읽어왔을 때 json이 strin..

about package.json

애플리케이션을 개발할 때 package.json 파일을 이용하면 사용하는 확장 모듈에 대해 의존성 관리가 가능해서 편하다. package.json 파일은 기본적으로 CommonJS 명세를 따르며 JSON 형식 파일이다. 직접 작성할 수도 있고, npm init 명령어를 통해 자동으로 생성할 수 있다. 해당 애플리케이션을 위한 확장 모듈 정보는 npm install -save를 통해 모듈 정보를 추가할 수 있다. npm init npm install -save 1. 예시 { "name" : "test", "description" : "javascript's test programming.", "keywords" : ["util", "f", "server", "client", "browser"], "auth..

Development/Node.js 2020.07.29
반응형