最简单的解决方案是提取
Category
道具使用地图。查看下面的代码。
还有另一种方法可以使用ES6 Set从数组中删除重复项
uniq = […new Set(array)]
var testjson = {
“d”: {
“results”: [{
“Title”: “Aardvark”,
“Category”: “Animals”,
“Description”: “My Test description”,
“TopTrainingCourse”: false,
“ID”: 1,
“Modified”: “2019-03-05T20:13:46Z”,
“Created”: “2019-03-05T20:13:36Z”
}, {
“Title”: “Red Panda”,
“Category”: “Animals”,
“Description”: “Donec id dictum sem”,
“TopTrainingCourse”: true,
“ID”: 10,
“Modified”: “2019-03-06T21:08:25Z”,
“Created”: “2019-03-06T21:08:25Z”
}, {
“Title”: “Tennis”,
“Category”: “Sports”,
“Description”: “Mauris sagittis ligula”,
“TopTrainingCourse”: true,
“ID”: 11,
“Modified”: “2019-03-06T21:08:35Z”,
“Created”: “2019-03-06T21:08:35Z”
}]
}
}
const res = testjson.d.results
.map((obj) => obj.Category)
.filter((elem, index, self) => index === self.indexOf(elem))
.sort((a, b) => a - b)
console.log(res)