2017-12-08 9 views
-4

내 목적은 Javascript 개체에서 두 개의 값을 더받습니다. 것이 가능하다?

objTest["CT_1^PS_1^OP01"] = "test1"; 
objTest["CT_1^PS_1^OP02"] = "test2"; 
objTest["CT_1^PS_1^OP03"] = "test3"; 

하고 내 stirng는

string sFindVal = "CT_1^PS_1"; 

이다가 "CT_1^PS_1"로 시작하는 단어의 "값"을 가져올 수 있습니까?

"CT1^PS_1"키만 값 ("test1", "test2", "test3") 만 가져오고 싶습니다.

영어는 제 모국어가 아닙니다. 양해 바랍니다.

답변

1

한 가지 방법은 모든 값의 배열을 반환 Object.keys을 통해 reduce을 사용하는 것입니다 경우 키 includes 문자열

const objTest = {}; 
 

 
objTest["CT_1\\PS_1\\OP01"] = "test1"; 
 
objTest["CP_1\\PS_1\\OP01"] = "error"; 
 
objTest["CT_1\\PS_1\\OP02"] = "test2"; 
 
objTest["CT_1\\PS_1\\OP03"] = "test3"; 
 

 
const sFindVal = "CT_1\\PS_1"; 
 

 
const arr = Object.keys(objTest).reduce((arr, key) => { 
 
    if (key.includes(sFindVal)) arr.push(objTest[key]); 
 
    return arr; 
 
}, []); 
 

 
console.log(arr);