我在Angular 4应用程序中有json数组对象,我需要在我的JSON数组中找到唯一的空间类型。
Json Array:
[{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Hospital_Lab"}},
{"label":{"Space_Type":"Office_OpenOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},{},
{"label":{"Space_Type":"Office_PrivateOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Hospital_Lab"}},{"label":{"Space_Type":"Office_OpenOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_OpenOffice"}},{}]
我想删除所有重复的空间类型。任何人都可以告诉我如何获得。 我想要结果:
[{Idx:0,Label:"Office_PrivateOffice"},{Idx:1,Label:"Office_OpenOffice"}]
答案 0 :(得分:4)
您可以使用lodash方法 _.uniqWith
var jsonarray = [{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Hospital_Lab"}},
{"label":{"Space_Type":"Office_OpenOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},{},
{"label":{"Space_Type":"Office_PrivateOffice"}},
{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Hospital_Lab"}},{"label":{"Space_Type":"Office_OpenOffice"}},{"label":{"Space_Type":"Office_PrivateOffice"}},{"label":{"Space_Type":"Office_OpenOffice"}},{}];
var filtered = _.uniqWith(jsonarray, _.isEqual);
console.log(filtered);
<script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>
答案 1 :(得分:1)
也可以使用lodash方法CardValues = [ace, king, queen, jack, 10, 9, 8, 7, 6, 5, 4, 3, 2],
CardSuits = [spades,hearts,clubs,diamonds],
CartesianList = [{X, Y} || X <- CardValues, Y <- CardSuits ],
io:format("\nCartesianList:~p\n",[CartesianList]).
。使用这种方法的好处是你可以告诉lodash你要删除哪个元素的副本。
_.uniqBy(root, 'duplicateElement');
在Angular中,您需要下载包var newJsonFile = _.uniqBy(label, 'Space_Type');
console.log(newJsonFile);
lodash
然后导入
npm install lodash