将对象合并为一个的Object.assign({},item)不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将对象合并为一个的Object.assign({},item)不起作用相关的知识,希望对你有一定的参考价值。
在第一个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));
})();
答案
我认为你必须减少数组:
const data = [{a:1}, {b:2}];
const newData = data.reduce((acc,curr) => ({...acc, ...curr}), {});
console.log(newData); // { a: 1, b: 2 }
以上是关于将对象合并为一个的Object.assign({},item)不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Object.assign 合并多个对象的属性,如果是对象有同名属性,则后面对象的属性值覆盖前面的。