我当前正在从本地JSON文件渲染Labels
的列表(请参阅JSON代码段)。但是,有duplicates of them,我希望每个人都出现一次。我尝试实现Array.filter()
和其他一些方法来获得这一行:"Categories": obj.ResourceType.results.map(function(val) { return val.Label;
,但是关于我的语法的一些了解却很稀奇,因此没有呈现。
有什么想法吗?理想情况下,我想使用一些东西来替换代码的这一部分:
distinctArray(inp) {
let uniqueArr = [];
for(var i = 0; i < inp.length; i++) {
if(inp[i] == undefined) continue;
if(uniqueArr.indexOf(inp[i]) < 0) {
uniqueArr.push(inp[i]);
}
}
return uniqueArr;
}
,同时仍在其上方的本节中:
let categories = this.distinctArray(temp).sort();
$("#km-labels").html(categories.join("<br>"));
import $ from 'jquery';
import JSONfile from '../../../public/JSONfile.json';
import { basename } from 'path';
var categories = '';
export default class {
constructor() {
this.loadData();
}
// ------------------------------------ //
loadData() {
let res = JSONfile.d.results.filter(function(val) {
return (val.FileLeafRef.trim().length > 0);
}).map(function(obj) {
return {
"FileName": obj.FileLeafRef,
"Path": obj.EncodedAbsUrl,
"Categories": obj.ResourceType.results.map(function(val) {
return val.Label;
}).join(";")
};
});
// debugger;
let temp = JSONfile.d.results.filter(function(val) {
return (val.FileLeafRef.trim().length > 0);
}).map(function(obj) {
return obj.ResourceType.results.map(function(val) {
return val.Label;
})
});
let categories = this.distinctArray(temp).sort();
$("#km-labels").html(categories.join("<br>"));
console.log(categories);
} // ------------- loadData
// ------------------------------------ //
distinctArray(inp){
let uniqueArr = [];
for(var i = 0; i < inp.length; i++) {
if(inp[i] == undefined) continue;
if(uniqueArr.indexOf(inp[i]) < 0) {
uniqueArr.push(inp[i]);
}
}
return uniqueArr;
}
{
"d": {
"results": [
{
"__metadata": {
"id": "[redacted]",
"uri": "[redacted]",
...
"type": "[redacted]"
},
"File": {
"__metadata": {
"id": "[redacted]",
"uri": "[redacted]",
"type": "SP.File"
},
"Name": "[redacted]"
},
"FileLeafRef": "[redacted]",
"ResourceType": {
"__metadata": {
"type": "Collection([redacted])"
},
"results": [
{
"Label": "Guides \uff06 Protocols",
...
...
}
]
},
"EncodedAbsUrl": "[redacted]"
},
{
"__metadata": {
"id": "[redacted]",
"uri": "[redacted]",
...
"type": "[redacted]"
},
"File": {
"__metadata": {
"id": "[redacted]",
"uri": "[redacted]",
"type": "[redacted]"
},
"Name": "[redacted]"
},
"FileLeafRef": "[redacted]",
"ResourceType": {
"__metadata": {
"type": "[redacted]"
},
"results": [
{
"Label": "Templates",
"TermGuid": "[redacted]",
...
},
{
"Label": "Guides \uff06 Protocols",
"TermGuid": "[redacted]",
...
}
]
},
"EncodedAbsUrl": "[redacted]"
},
...
...
let unique = {};
let temp = JSONfile.d.results.filter(function(val) {
return (val.FileLeafRef.trim().length > 0);
}).forEach(function(obj) {
obj.ResourceType.results.forEach(function(val) {
unique[val.Label] = val;
})
}).map(function(obj) {
return obj.ResourceType.results.map(function(val) {
return val.Label;
})
});
答案 0 :(得分:0)
使用键为标签的对象。由于对象键是唯一的,因此该对象的值将没有任何重复的标签。
let unique = {};
JSONfile.d.results.filter(function(val) {
return (val.FileLeafRef.trim().length > 0);
}).forEach(function(obj) {
obj.ResourceType.results.forEach(function(val) {
unique[val.Label] = true; // value can be anything, only keys matter
})
});
let categories = Object.keys(unique).sort();