2017-05-18 5 views
0

객체를 반환해야하는 함수를 수행 중입니다. 이 경우 query이 초기 개체이며, 더 많은 키/값을 (예 : query.location.city)에 추가하지만 .으로 중첩하여 값을 할당 할 수없는 것처럼 보입니다.자바 객체의 중첩 된 kay 값 쌍

어떻게 이것을 수행 할 수 있습니까?

const _ = require('lodash'); 

let queryBuilder = function (obj) { 

    let query = {}; 

    // City 
    if(_.has(obj, 'city')) { 
    query.location.city = obj.city ; 
    } 

    // Country 
    if(_.has(obj, 'country')) { 
    query.location.country_code = obj.country; 
    } 

    return query; 

}; 

module.exports = queryBuilder; 
+3

잘 '위치'가 없으므로 추가해야합니다. 'query.location = query.location || {}' – epascarello

답변

4

obj 위치를 만들어야합니다.

const _ = require('lodash'); 

let queryBuilder = function (obj) { 

    let query = {}; 

    // City 
    if(_.has(obj, 'city')) { 
    query.location = { 
     city: obj.city 
    }; 
    } 

    return query; 

}; 

module.exports = queryBuilder;