如何在嵌套对象中映射特定的键值

时间:2019-01-15 20:36:45

标签: javascript arrays object

我在下面有这个对象

var data = {
  "results": [
    {
      "_id": "5c3e2e2565d9440e8e4fa3f3",
      "team1": 2,
      "team2": 4,
      "createdAt": "2019-01-15T19:01:57.842Z",
      "updatedAt": "2019-01-15T19:01:58.020Z",
      "__v": 0,
      "user": "5c385ff34ae2c90d18fcb95f"
    },
    {
      "_id": "5c3e2e2865d9440e8e4fa3f4",
      "team1": 3,
      "team2": 2,
      "createdAt": "2019-01-15T19:02:00.480Z",
      "updatedAt": "2019-01-15T19:02:00.649Z",
      "__v": 0,
      "user": "5c385ff34ae2c90d18fcb95f"
    },
    {
      "_id": "5c3e2e5265d9440e8e4fa3f5",
      "team1": 1,
      "team2": 3,
      "createdAt": "2019-01-15T19:02:42.642Z",
      "updatedAt": "2019-01-15T19:02:42.814Z",
      "__v": 0,
      "user": "5c385ff34ae2c90d18fcb95f"
    }
  ]
}

我要做的就是获取结果中的每个team1和team2值,并使用map()创建它,到目前为止,我正在使用此代码。

const array = Object.values(data.results).map(o => Object.values(o))    

这适用于所有值,但我只需要team1和team2而不需要其他,那么该怎么做?

我的预期输出是 [[2,4],[3,2],[1、3]]

3 个答案:

答案 0 :(得分:4)

您可以通过直接映射数组来破坏所需的属性。

var data = { results: [{ _id: "5c3e2e2565d9440e8e4fa3f3", team1: 2, team2: 4, createdAt: "2019-01-15T19:01:57.842Z", updatedAt: "2019-01-15T19:01:58.020Z", __v: 0, user: "5c385ff34ae2c90d18fcb95f" }, { _id: "5c3e2e2865d9440e8e4fa3f4", team1: 3, team2: 2, createdAt: "2019-01-15T19:02:00.480Z", updatedAt: "2019-01-15T19:02:00.649Z", __v: 0, user: "5c385ff34ae2c90d18fcb95f" }, { _id: "5c3e2e5265d9440e8e4fa3f5", team1: 1, team2: 3, createdAt: "2019-01-15T19:02:42.642Z", updatedAt: "2019-01-15T19:02:42.814Z", __v: 0, user: "5c385ff34ae2c90d18fcb95f" }] },
    result = data.results.map(({ team1, team2 }) => [team1, team2]);
    
console.log(result);

答案 1 :(得分:3)

您需要显式提取这些值:

const data = {"results":[{"_id":"5c3e2e2565d9440e8e4fa3f3","team1":2,"team2":4,"createdAt":"2019-01-15T19:01:57.842Z","updatedAt":"2019-01-15T19:01:58.020Z","__v":0,"user":"5c385ff34ae2c90d18fcb95f"},{"_id":"5c3e2e2865d9440e8e4fa3f4","team1":3,"team2":2,"createdAt":"2019-01-15T19:02:00.480Z","updatedAt":"2019-01-15T19:02:00.649Z","__v":0,"user":"5c385ff34ae2c90d18fcb95f"},{"_id":"5c3e2e5265d9440e8e4fa3f5","team1":1,"team2":3,"createdAt":"2019-01-15T19:02:42.642Z","updatedAt":"2019-01-15T19:02:42.814Z","__v":0,"user":"5c385ff34ae2c90d18fcb95f"}]}

const array = data.results.map(o => [o.team1, o.team2])

console.log(array)

如果有大量值,则可以创建一个函数,该函数从一个对象中提取一个值列表,然后将其用作地图的回调:

const data = {"results":[{"_id":"5c3e2e2565d9440e8e4fa3f3","team1":2,"team2":4,"createdAt":"2019-01-15T19:01:57.842Z","updatedAt":"2019-01-15T19:01:58.020Z","__v":0,"user":"5c385ff34ae2c90d18fcb95f"},{"_id":"5c3e2e2865d9440e8e4fa3f4","team1":3,"team2":2,"createdAt":"2019-01-15T19:02:00.480Z","updatedAt":"2019-01-15T19:02:00.649Z","__v":0,"user":"5c385ff34ae2c90d18fcb95f"},{"_id":"5c3e2e5265d9440e8e4fa3f5","team1":1,"team2":3,"createdAt":"2019-01-15T19:02:42.642Z","updatedAt":"2019-01-15T19:02:42.814Z","__v":0,"user":"5c385ff34ae2c90d18fcb95f"}]}

const getValues = keys => o => keys.map(key => o[key], {});
const array = data.results.map(getValues(['team1', 'team2']));

console.log(array)

答案 2 :(得分:2)

您可以使用array: reduce遍历每个对象,拉出所需的两个属性,然后将它们推入结果累加器数组。

const data = {
  "results": [
    {
      "_id": "5c3e2e2565d9440e8e4fa3f3",
      "team1": 2,
      "team2": 4,
      "createdAt": "2019-01-15T19:01:57.842Z",
      "updatedAt": "2019-01-15T19:01:58.020Z",
      "__v": 0,
      "user": "5c385ff34ae2c90d18fcb95f"
    },
    {
      "_id": "5c3e2e2865d9440e8e4fa3f4",
      "team1": 3,
      "team2": 2,
      "createdAt": "2019-01-15T19:02:00.480Z",
      "updatedAt": "2019-01-15T19:02:00.649Z",
      "__v": 0,
      "user": "5c385ff34ae2c90d18fcb95f"
    },
    {
      "_id": "5c3e2e5265d9440e8e4fa3f5",
      "team1": 1,
      "team2": 3,
      "createdAt": "2019-01-15T19:02:42.642Z",
      "updatedAt": "2019-01-15T19:02:42.814Z",
      "__v": 0,
      "user": "5c385ff34ae2c90d18fcb95f"
    }
  ]
}

const processData = (data) => data.reduce((acc, {team1, team2}) => { // acc is accumulator array, destructure data object into properties you want
  acc.push([team1, team2]); // create new array of teams 1 and 2 and push into accumulator
  return acc;
}, []);

console.log(processData(data.results));