如何从数组中提取指定的对象?

时间:2018-06-18 12:18:27

标签: javascript

我尝试从每个对象中提取作业对象,但似乎某些东西不起作用,也没有使用console.log。如果我使用result从console.log更改response并对上面的注释进行评论,我将收到以下文档

[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."));
  });
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解,但我认为问题是jobs是一个数组,而不是一个对象,所以这应该有效:

var result = [];

response.map(o=>{
result.push(o.jobs);
   });

   console.log(result);