가격순 정렬을 하는중 두개의 json파일을 하나로 합쳐서 데이타파일을 하나만들어 사용해야 겠다는생각에
mergeFetch라는 함수를 만들었습니다.
function mergeFetch(fetch1, fetch2) {
const fetchReq1 = fetch(fetch1).then((res) => res.json());
const fetchReq2 = fetch(fetch2).then((res) => res.json());
const allData = Promise.all([fetchReq1, fetchReq2]);
let allDataArray = [];
allData.then((res) => {
res.forEach((item) => {
allDataArray.push(...item);
});
return allDataArray;
});
console.log(allDataArray);
}
const data = mergeFetch(
'https://codingapple1.github.io/js/more1.json',
'https://codingapple1.github.io/js/more2.json'
);
console.log(data);
그런데 console.log(allDataArray)하면 allDataArray 배열에 push한 값이 들어지 않고 []빈객체럼 나옵니다. 분명 값이 있는거 같은데..
data에 함수값을 할당해 보면 undefined라고 나옵니다. 이걸 어떻게 해야 mergeFetch()에서 리턴된 배열을 가져올수 있을까요?