forEach()
에 의해 생성 된 객체 배열과 newData를 어떻게 구합니까? 전 세계적으로 var result = []
을 정의하고 console.log(result);
array.protoype.foreEach()의 결과를 정의되지 않은 변수에 저장
var paragraphs = ["this is a p", "%%this is an h2", "%this is an h1"];
var newData = paragraphs.forEach(function(x) {
var result = [];
var header = 0;
while (x.charAt(header) === "%")
header++;
if (header > 0) {
result.push({type:"h" + header, content: x.slice(header)});
} else {
result.push({type: "p", content: x});
}
return result;
});
console.log(newData); // undefined
forEach는 for 루프와 같습니다. 당신은'map'을 원합니다. – Blender
Thx -지도에 익숙합니다. - 죄송합니다. - 나는주의해야합니다. 그러나이 경우 값을 반환 할 수없는 이유를 이해하려고 노력하고 있습니다. 그것의 다만 저를 도청하는 - 또는 나는 다만 그것을 떨어 뜨려야한다. – jamie
여기를보세요 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach –