Reputation: 495
I have this object below
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"
}
]
}
all I want is to get each team1 and team2 values in results and create it with map(), so far I am using this code.
const array = Object.values(data.results).map(o => Object.values(o))
which works for all the values but I only need team1 and team2 and not the others, so how to do it?
my expected output is [[2, 4], [3, 2], [1, 3]]
Upvotes: 1
Views: 77
Reputation: 202608
You can use array: reduce
to iterate over each object, pull out the two properties you want, and push them into the result accumulator 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 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));
Upvotes: 2
Reputation: 191946
You need to explicitly extract those values:
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)
If you've got a large number of values, you can create a function that extract a list of values from an object, and then use it as the map's callback:
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)
Upvotes: 3
Reputation: 386540
You could destrucure the wanted properties by mapping the array directly.
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);
Upvotes: 4