다음과 같은 배열이 있습니다.
["gender-m", "age-20", "city-london", "lang-en", "support-home"]
JSON 개체를 생성하려고했습니다.
{"gender":"m", "age":"20", "city":"london", "lang":"en", "support":"home"}
내가 생각할 수있는 한 가지 해결책은 FOR 루프를 사용하여 만드는 것이지만 이에 대한 우아한 해결책이 있다고 확신합니다. 어떤 제안이라도 제발 도와주세요.
당신이 걸릴 수 Object.fromEntries
스플릿 키 / 값 쌍.
var data = ["gender-m", "age-20", "city-london", "lang-en", "support-home"],
result = Object.fromEntries(data.map(s => s.split('-')));
console.log(result);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다