In first API call I'm getting an array of IDs to be able to make another API call to grab revenue key/value pairs, then push it back to the main object(as it doesn't have it by default).
const mainURL =
"https://api.themoviedb.org/3/discover/movie?api_key=2339027b84839948cd9be5de8b2b36da&language=en-US&sort_by=revenue.desc&include_adult=false&include_video=false&page=";
const movieBaseURL =
"https://api.themoviedb.org/3/movie/335?api_key=2339027b84839948cd9be5de8b2b36da&language=en-US";
const apiKey = "2339027b84839948cd9be5de8b2b36da";
let getRevenueByID,revenue, data, response, obj;
let Each_ID_Array = [],
All_ID_Array = [],
All_ID_Array_merged = [],
pages_Array = [1, 2],
keyValueArr = [],
allKeyValueArr = [],
objectArr = [];
responses = [];
newData = [];
let text = "";
//fetch function
let getSearchResultsForOnePage = url => {
//fetch
return fetch(url);
};
//Function to cool down requests
let pause = time => {
// handy pause function to await
return new Promise(resolve => setTimeout(resolve, time));
};
//Get main object for d3 visualisation
getAllSearchResultProfiles = async searchURL => {
let URLs = [];
for (let i = 0; i < pages_Array.length; i++) {
URLs.push(searchURL + pages_Array[i]);
}
console.log(URLs);
for (let url of URLs) {
console.log("sending request");
response = await getSearchResultsForOnePage(url);
console.log("received", response);
responses.push(await response.json());
await pause(500);
}
console.log('responses', responses);
return responses;
};
//Function to get revenue by ID
getRevenueByID = async (arr) => {
let resObj = {}; // Is an complete result
for (let i = 0; i < arr.length; i++) {
console.log("ID is: ", arr[i]);
getRevenueURL = await fetch(
"https://api.themoviedb.org/3/movie/" +
arr[i] +
"?api_key=" +
apiKey +
"&language=en-US"
);
let data = await getRevenueURL.json();
console.log("data received: ", data);
await pause(200);
//return key/value array
keyValueArr = Object.entries(data)[16];
console.table(keyValueArr);
allKeyValueArr.push(keyValueArr);
//convert key/value array to object
obj = Object.assign(
...allKeyValueArr.map(d => ({
[d[0]]: d[1]
}))
);
console.log("object is: ", obj);
resObj[arr[i]] = obj;
console.log("resObj: ", resObj[arr[i]]);
}
console.log("OUTPUT - getRevenueByID: ", resObj);
return resObj;
};
(getFinalObject = async () => {
getAllSearchResultProfiles(mainURL).then(async (data) => {
//get movie ID's
for (let i = 0; i < data.length; i++) {
Each_ID_Array = data[i].results.map(d => d.id);
All_ID_Array.push(Each_ID_Array);
console.table('All ID Array', All_ID_Array);
All_ID_Array_merged = [].concat.apply([], All_ID_Array);
console.table('All ID Array merged',All_ID_Array_merged);
console.log('data[i].results: ',data[i].results);
obj = await getRevenueByID(All_ID_Array_merged);
console.log('obj is', obj);
//inject revenue key/value
Object.keys(data[i].results).forEach(key => {
console.log("The name of the current key: ", key);
console.log("The value of the current key: ", data[i].results[key]);
data[i].results[key]["revenue"] = obj[data[i].results[key].id]["revenue"];
console.log('data[i].results[key]["revenue"]: ', data[i].results[key]["revenue"]);
});
}
console.log('data', data);
Data I'm getting is in the following format [{…}, {…}]. So instead of an array of objects I'm trying to get an array of one object [{...}] inside
newData = data.map(item =>
Object.assign({}, item)
);
console.log('data for visualisation is:',newData);
}).catch(error => console.log(error));
})();
I think you have to reduce the array:
const data = [{a:1}, {b:2}];
const newData = data.reduce((acc,curr) => ({...acc, ...curr}), {});
console.log(newData); // { a: 1, b: 2 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With