[英]How to extract an specified object from an array?
我嘗試從每個對象中提取作業對象,但是似乎有些東西不起作用,也沒有console.log。 如果更改帶有response
console.log的result
並在上面的行中添加注釋,我將收到以下文檔
[13:01:53] Array [
[13:01:53] Object {
[13:01:53] "_id": "5b276024009b5409f30703ef",
[13:01:53] "jobs": Array [
[13:01:53] Object {
[13:01:53] "_id": "5b276afd8d05600a78e91fea",
[13:01:53] "description": "",
[13:01:53] "location": Array [
[13:01:53] Object {
[13:01:53] "_id": "5b276afd8d05600a78e91feb",
[13:01:53] },
[13:01:53] ],
[13:01:53] "postDate": "2018-06-18T08:18:16.893Z",
[13:01:53] },
[13:01:53] Object {
[13:01:53] "_id": "5b276c35bab2220ae8ec8859",
[13:01:53] "description": "",
[13:01:53] "location": Array [
[13:01:53] Object {
[13:01:53] "_id": "5b276c35bab2220ae8ec885a",
[13:01:53] },
[13:01:53] ],
[13:01:53] "postDate": "2018-06-18T08:18:16.893Z",
[13:01:53] },
[13:01:53] ],
[13:01:53] },
[13:01:53] Object {
[13:01:53] "_id": "5b277edbcb87d00c30a3a40f",
[13:01:53] "jobs": Array [],
[13:01:53] },
[13:01:53] ]
這是我嘗試實現的功能
export function getAllJobs(dispatch, getState) {
const state = getState();
const { token } = state.auth;
return axios.get(JOBS_ALL_URL, {
headers: { authorization: token }
}).then((response) => {
const { result } = response.map(obj => obj.jobs);
console.log(result)
dispatch(setJobs(result));
}).catch((err) => {
dispatch(console.log("Couldn't get jobs."));
});
}
我不確定是否理解,但是我認為問題是jobs
是一個數組,而不是對象,因此這應該可以工作:
var result = [];
response.map(o=>{
result.push(o.jobs);
});
console.log(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.