이 같은 JSON을 감안할 때 :json을 들여 쓰기 된 텍스트 트리로 효율적으로 변환하려면 어떻게해야합니까?
{
Normalize: {},
Typography: {},
'Flex-grid': {},
Elements: {},
Forms: {},
Navigation: {
Links: {},
Menus: {}
},
Accessibility: {},
Alignments: {},
Clearings: {},
Widgets: {},
Content: {
'Posts-and-pages': {},
Comments: {}
},
'Infinite-scroll': {},
Media: {
Captions: {},
Galleries: {}
}
}
나는이 같은 들여 쓰기 일반 텍스트 생성하려면 : 나는 몇 가지 빠른 더러운 코드로 올라와있다
# Normalize
# Typography
# Flex grid
# Elements
# Forms
# Navigation
## Links
## Menus
# Accessibility
# Alignments
# Clearings
# Widgets
# Content
## Posts and pages
## Comments
# Infinite scroll
# Media
## Captions
## Galleries
를 (I는 자바 스크립트 초보자 해요) 이것을 달성하기 위해. 코드를 실행하면 속도가 매우 느려집니다. 이 작업을보다 빠르고 효율적으로 수행하려면 어떻게해야합니까? 여기
function jsonToIndentedText(json) {
var text = JSON.stringify(json, null, '\t');
var indentedText = [];
var lines = text.split('\n');
lines.forEach(function(line) {
if ('{' == line || '{' == line || '' == line) {
return;
}
var textNow = line.replace(/"/gi, '').replace(/:[\s | \S]*$/i, '');
if (textNow.search("}") < 0) {
textNowArr = textNow.split(/\t/gi);
level = textNowArr.length - 1;
var indents = '';
var hashes = '#';
for (var i = 1; i < level; i++) {
indents += '\t';
hashes += '#';
}
indentedText.push(indents + hashes + ' ' + textNow.replace(/\t/gi, '').replace(/-/gi,' '));
}
});
return indentedText.join('\n');
}
내 jsfiddle입니다 :
https://jsfiddle.net/94t4dyag/1/
내가이 조금 편집과 속임수를 썼는지 – Hackerman