将对象合并为一个的Object.assign({},item)不起作用

时间:2019-03-15 12:08:21

标签: javascript json object async-await

在第一个API调用中,我得到一个ID数组,以便能够进行另一个API调用以获取收入键/值对,然后将其推回主对象(因为默认情况下没有)

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);

我获取的数据采用以下格式[{…},{…}]。因此,我尝试使用的不是对象的数组,而是使用内部的一个对象 [{...}]的数组

      newData = data.map(item => 
        Object.assign({}, item)
        );

      console.log('data for visualisation is:',newData);
    }).catch(error => console.log(error));


})();

1 个答案:

答案 0 :(得分:3)

我认为您必须减少数组:

 const data = [{a:1}, {b:2}];
 
 const newData = data.reduce((acc,curr) => ({...acc, ...curr}), {});
  
 console.log(newData); // { a: 1, b: 2 }