我正在尝试对 json 唯一值进行排序。这是一个例子:
let arif = [{
"name": "Arif",
"label": "arif"
},
{
"name": "Hasan",
"label": "arif"
},
{
"name": "Tomal",
"label": "tomal"
},
{
"name": "Biswas",
"label": "tomal"
},
{
"name": "Maruf",
"label": "maruf"
},
{
"name": "Rahman",
"label": "maruf"
},
{
"name": "Iqbal",
"label": "maruf"
}
];
console.log(arif);
let myvalue;
if (arif) {
myvalue = _.uniq(arif, function (arif) {
return arif.label;
});
}
console.log(myvalue);
在上面的代码中我使用了 underscore获取json变量label
的唯一值。我想将 label
的每个值存储在另一个值中,如下所示:
let newvalue1 = myvalue[0].label;
我知道我可以获得 myvalue
的长度,但是如何动态存储该值而不是使用 myvalue[0]
?
最佳答案
这里有两件事。
首先求uniq值
let uniqArr = _.uniq(arif, function (item) { return item.label; });
然后对 uniq 值 arr 进行排序 uniqArr
let sortArr = _.sortBy( uniqArr, function( item ) { return item.label; } );
所以在sortArr
中你会得到
let newvalue2=sortArr[1].label
let newvalue3=sortArr[2].label
或者你可以使用_.each
循环
关于Javascript Json唯一值排序和存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52585800/