I have this object:
{
"apple": {
"0": {
"2018-04-25 19:51:38": {
"x": "38.0",
"y": "23.0"
},
"2018-04-25 19:51:39": {
"x": "NaN",
"y": "NaN"
},
"2018-04-25 19:51:40": {
"x": "NaN",
"y": "NaN"
}
},
"5": {
"2018-04-25 19:51:38": {
"x": "50.0",
"y": "35.0"
},
"2018-04-25 19:51:43": {
"x": "21.0",
"y": "3.0"
}
},
"6": {
"2018-04-25 19:51:34": {
"x": "30.0",
"y": "15.0"
},
"2018-04-25 19:51:39": {
"x": "NaN",
"y": "NaN"
},
"2018-04-25 19:52:40": {
"x": "22.0",
"y": "20.0"
},
"2018-04-25 19:52:42": {
"x": "33.0",
"y": "45.0"
}
}
},
"team": {
"2": {
"2018-04-25 19:51:35": {
"x": "32.0",
"y": "25.0"
},
"2018-04-25 19:51:36": {
"x": "33.0",
"y": "40.0"
},
"2018-04-25 19:51:37": {
"x": "12.0",
"y": "24.0"
},
"2018-04-25 19:51:38": {
"x": "33.0",
"y": "45.0"
}
},
"3": {
"2018-04-25 19:51:35": {
"x": "2.0",
"y": "3.0"
},
"2018-04-25 19:51:36": {
"x": "4.0",
"y": "5.0"
},
"2018-04-25 19:51:37": {
"x": "12.0",
"y": "15.0"
},
"2018-04-25 19:51:38": {
"x": "33.0",
"y": "45.0"
}
},
"4": {
"2018-04-25 19:51:35": {
"x": "20.0",
"y": "30.0"
},
"2018-04-25 19:51:36": {
"x": "41.0",
"y": "35.0"
},
"2018-04-25 19:51:37": {
"x": "32.0",
"y": "65.0"
},
"2018-04-25 19:51:38": {
"x": "43.0",
"y": "49.0"
}
},
"5": {
"2018-04-25 19:51:35": {
"x": "21.0",
"y": "33.0"
},
"2018-04-25 19:51:36": {
"x": "31.0",
"y": "12.0"
},
"2018-04-25 19:51:37": {
"x": "34.0",
"y": "54.0"
},
"2018-04-25 19:51:38": {
"x": "93.0",
"y": "22.0"
}
}
}
}
What I want are two array of unique timestamps: one for apple
and one for team
. So:
const appleTimestamps = ["2018-04-25 19:51:34", "2018-04-25 19:51:37", "2018-04-25 19:51:38", "2018-04-25 19:51:39", "2018-04-25 19:51:40", "2018-04-25 19:51:43", "2018-04-25 19:52:40", "2018-04-25 19:52:42"]
const teamTimestamps = ["2018-04-25 19:51:35", "2018-04-25 19:51:36", "2018-04-25 19:51:37", "2018-04-25 19:51:38"]
As you can see, keys inside Apple and team are not sequential. How can I do that? The only way i thought is use find
but I'm sure I'm wrong.
(I can use also Lodash )
Perhaps something like this:
const timestamps = obj => [...new Set( Object.values(obj) .map(Object.keys) .reduce((a, b) => a.concat(b), []) // someday: `flatten` )] const obj = {"apple": {"0": {"2018-04-25 19:51:38": {"x": "38.0", "y": "23.0"}, "2018-04-25 19:51:39": {"x": "NaN", "y": "NaN"}, "2018-04-25 19:51:40": {"x": "NaN", "y": "NaN"}}, "5": {"2018-04-25 19:51:38": {"x": "50.0", "y": "35.0"}, "2018-04-25 19:51:43": {"x": "21.0", "y": "3.0"}}, "6": {"2018-04-25 19:51:34": {"x": "30.0", "y": "15.0"}, "2018-04-25 19:51:39": {"x": "NaN", "y": "NaN"}, "2018-04-25 19:52:40": {"x": "22.0", "y": "20.0"}, "2018-04-25 19:52:42": {"x": "33.0", "y": "45.0"}}}, "team": {"2": {"2018-04-25 19:51:35": {"x": "32.0", "y": "25.0"}, "2018-04-25 19:51:36": {"x": "33.0", "y": "40.0"}, "2018-04-25 19:51:37": {"x": "12.0", "y": "24.0"}, "2018-04-25 19:51:38": {"x": "33.0", "y": "45.0"}}, "3": {"2018-04-25 19:51:35": {"x": "2.0", "y": "3.0"}, "2018-04-25 19:51:36": {"x": "4.0", "y": "5.0"}, "2018-04-25 19:51:37": {"x": "12.0", "y": "15.0"}, "2018-04-25 19:51:38": {"x": "33.0", "y": "45.0"}}, "4": {"2018-04-25 19:51:35": {"x": "20.0", "y": "30.0"}, "2018-04-25 19:51:36": {"x": "41.0", "y": "35.0"}, "2018-04-25 19:51:37": {"x": "32.0", "y": "65.0"}, "2018-04-25 19:51:38": {"x": "43.0", "y": "49.0"}}, "5": {"2018-04-25 19:51:35": {"x": "21.0", "y": "33.0"}, "2018-04-25 19:51:36": {"x": "31.0", "y": "12.0"}, "2018-04-25 19:51:37": {"x": "34.0", "y": "54.0"}, "2018-04-25 19:51:38": {"x": "93.0", "y": "22.0"}}}} console.log(timestamps(obj.apple)) console.log(timestamps(obj.team))
Object.values
and Object.keys
make for great ways to traverse objects like this. And a Set
is the canonical way to ensure uniqueness.
You can refer the below code. If there are multiple objects like apple, sets then you can move the code to a function instead of writing again and again.
var obj = { "apple": { "0": { "2018-04-25 19:51:38": { "x": "38.0", "y": "23.0" }, "2018-04-25 19:51:39": { "x": "NaN", "y": "NaN" }, "2018-04-25 19:51:40": { "x": "NaN", "y": "NaN" } }, "5": { "2018-04-25 19:51:38": { "x": "50.0", "y": "35.0" }, "2018-04-25 19:51:43": { "x": "21.0", "y": "3.0" } }, "6": { "2018-04-25 19:51:34": { "x": "30.0", "y": "15.0" }, "2018-04-25 19:51:39": { "x": "NaN", "y": "NaN" }, "2018-04-25 19:52:40": { "x": "22.0", "y": "20.0" }, "2018-04-25 19:52:42": { "x": "33.0", "y": "45.0" } } }, "team": { "2": { "2018-04-25 19:51:35": { "x": "32.0", "y": "25.0" }, "2018-04-25 19:51:36": { "x": "33.0", "y": "40.0" }, "2018-04-25 19:51:37": { "x": "12.0", "y": "24.0" }, "2018-04-25 19:51:38": { "x": "33.0", "y": "45.0" } }, "3": { "2018-04-25 19:51:35": { "x": "2.0", "y": "3.0" }, "2018-04-25 19:51:36": { "x": "4.0", "y": "5.0" }, "2018-04-25 19:51:37": { "x": "12.0", "y": "15.0" }, "2018-04-25 19:51:38": { "x": "33.0", "y": "45.0" } }, "4": { "2018-04-25 19:51:35": { "x": "20.0", "y": "30.0" }, "2018-04-25 19:51:36": { "x": "41.0", "y": "35.0" }, "2018-04-25 19:51:37": { "x": "32.0", "y": "65.0" }, "2018-04-25 19:51:38": { "x": "43.0", "y": "49.0" } }, "5": { "2018-04-25 19:51:35": { "x": "21.0", "y": "33.0" }, "2018-04-25 19:51:36": { "x": "31.0", "y": "12.0" }, "2018-04-25 19:51:37": { "x": "34.0", "y": "54.0" }, "2018-04-25 19:51:38": { "x": "93.0", "y": "22.0" } } } }; var k = []; Object.keys(obj.apple).forEach((el)=>{ k = k.concat(Object.keys(obj.apple[el])); }); var appleTimeStamps = new Set(k); appleTimeStamps = Array.from(appleTimeStamps); console.log(appleTimeStamps); var j = []; Object.keys(obj.team).forEach((el)=>{ j = j.concat(Object.keys(obj.team[el])); }); var teamTimeStamps = new Set(j); teamTimeStamps = Array.from(teamTimeStamps); console.log(teamTimeStamps);
Using lodash, you can create the function using _.flow()
:
_.flatMap()
with _.keys()
to get an array of timestamps _.uniq()
to remove duplicates const timestamps = _.flow([ o => _.flatMap(o, _.keys), _.uniq, ]) const obj = {"apple": {"0": {"2018-04-25 19:51:38": {"x": "38.0", "y": "23.0"}, "2018-04-25 19:51:39": {"x": "NaN", "y": "NaN"}, "2018-04-25 19:51:40": {"x": "NaN", "y": "NaN"}}, "5": {"2018-04-25 19:51:38": {"x": "50.0", "y": "35.0"}, "2018-04-25 19:51:43": {"x": "21.0", "y": "3.0"}}, "6": {"2018-04-25 19:51:34": {"x": "30.0", "y": "15.0"}, "2018-04-25 19:51:39": {"x": "NaN", "y": "NaN"}, "2018-04-25 19:52:40": {"x": "22.0", "y": "20.0"}, "2018-04-25 19:52:42": {"x": "33.0", "y": "45.0"}}}, "team": {"2": {"2018-04-25 19:51:35": {"x": "32.0", "y": "25.0"}, "2018-04-25 19:51:36": {"x": "33.0", "y": "40.0"}, "2018-04-25 19:51:37": {"x": "12.0", "y": "24.0"}, "2018-04-25 19:51:38": {"x": "33.0", "y": "45.0"}}, "3": {"2018-04-25 19:51:35": {"x": "2.0", "y": "3.0"}, "2018-04-25 19:51:36": {"x": "4.0", "y": "5.0"}, "2018-04-25 19:51:37": {"x": "12.0", "y": "15.0"}, "2018-04-25 19:51:38": {"x": "33.0", "y": "45.0"}}, "4": {"2018-04-25 19:51:35": {"x": "20.0", "y": "30.0"}, "2018-04-25 19:51:36": {"x": "41.0", "y": "35.0"}, "2018-04-25 19:51:37": {"x": "32.0", "y": "65.0"}, "2018-04-25 19:51:38": {"x": "43.0", "y": "49.0"}}, "5": {"2018-04-25 19:51:35": {"x": "21.0", "y": "33.0"}, "2018-04-25 19:51:36": {"x": "31.0", "y": "12.0"}, "2018-04-25 19:51:37": {"x": "34.0", "y": "54.0"}, "2018-04-25 19:51:38": {"x": "93.0", "y": "22.0"}}}} console.log(timestamps(obj.apple)) console.log(timestamps(obj.team))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.