[ [{“name”:“123”,“url”:“fdsfds”,“type”:“x”},{“name”:“123”,“url”:“fdsfds”,“type”:“y”},{“name”:“123”,“url”:“fdsfds”,“type”:“z”}], [{“name”:“123”,“url”:“fdsfds”,“type”:“x”},{“name”:“123”,“url”:“fdsfds”,“type”:“y”}] ]
将这个从新排按type分类排列
[ [{“name”:“123”,“url”:“fdsfds”,“type”:“x”},{“name”:“123”,“url”:“fdsfds”,“type”:“x”}], [{“name”:“123”,"ur"l:“fdsfds”,“type”:“y”},{“name”:“123”,“url”:“fdsfds”,“type”:“y”}], [{“name”:“123”,“url”:“fdsfds”,“type”:“z”}] ]
格式已经修改就这个意思 大神凑合着看
先把上面的数据格式写正确吧。
大神还没来吗
三个数组对应type[x,y,z],遍历上面的,按照type,分别放入不同的数组。代码就不写了。
coordcn 3楼•40分钟前
三个数组对应type[x,y,z],遍历上面的,按照type,分别放入不同的数组。代码就不写了。
那xyz 只是我随便写的可能还有abc 只类的
遍历的但是无法分类 我试了好多方法了 都无法分类
就是按照json里的一个属性归类一起在排列出来
https://lodash.com/docs#sortBy 这个我看了还是没解决我的问题
var users = [ { ‘user’: ‘barney’, ‘age’: 36 }, { ‘user’: ‘fred’, ‘age’: 40 }, { ‘user’: ‘barney’, ‘age’: 26 }, { ‘user’: ‘fred’, ‘age’: 30 } ];
.map(.sortByAll(users, [‘user’, ‘age’]), _.values); // → [[‘barney’, 26], [‘barney’, 36], [‘fred’, 30], [‘fred’, 40]]
不是我要的结果 我要的结果可以是这样的
// → [[‘barney’, 26], [‘barney’, 36]],[ [‘fred’, 30], [‘fred’, 40]]
试试 http://github.com/alsotang/tableman 的 group 方法
lodash的flatten, groupBy, map可以搞定,参考: https://gist.github.com/xavierchow/342be76041980e36e663
一个笨办法
var a = arr.reduce(function(p1, p2) { // flatten
return p1.concat(p2);
}).reduce(function(v1, v2) { // groupBy
var eleExists = v1.some(function(e) {
if(e[0]['type'] === v2['type']) {
e.push(v2);
return true;
} else {
return false;
}
});
if(!eleExists) v1.push([v2]);
return v1;
}, []).sort(function(s1, s2) { // sort
return s1[0]['type'] > s2[0]['type'];
});
console.log(a);
@alsotang benchmark不错,嘻嘻
function resort(array) {
var i = 0,
length = array.length,
result = [],
indexes = {},
j, row, item, type;
while ((row = array[i++]) instanceof Array) {
j = 0;
while (typeof (item = row[j++]) === 'object'
&& item !== null
&& 'type' in item) {
type = item.type;
if (type in indexes) {
result[indexes[type]].push(item);
} else {
indexes[type] = result.length;
result.push([item]);
}
}
}
return result;
}
console.log('\nThe resort 1 is: \n\n', resort([
[{"name":"123","url":"fdsfds","type":"x"},
{"name":"123","url":"fdsfds","type":"y"},
{"name":"123","url":"fdsfds","type":"z"}],
[{"name":"123","url":"fdsfds","type":"x"},
{"name":"123","url":"fdsfds","type":"y"}]
]));
console.log('\nThe resort 2 is: \n\n', resort([
[{"name":"123","url":"fdsfds","type":"x"},
{"name":"123","url":"fdsfds","type":"y"},
{"name":"123","url":"fdsfds","type":"z"},
{"name":"123","url":"fdsfds","type":"z"}],
[{"name":"123","url":"fdsfds","type":"x"},
{"name":"123","url":"fdsfds","type":"y"}],
[{"name":"123","url":"fdsfds","type":"x"}]
]));
~$ node test
The resort 1 is:
[ [ { name: '123', url: 'fdsfds', type: 'x' },
{ name: '123', url: 'fdsfds', type: 'x' } ],
[ { name: '123', url: 'fdsfds', type: 'y' },
{ name: '123', url: 'fdsfds', type: 'y' } ],
[ { name: '123', url: 'fdsfds', type: 'z' } ] ]
The resort 2 is:
[ [ { name: '123', url: 'fdsfds', type: 'x' },
{ name: '123', url: 'fdsfds', type: 'x' },
{ name: '123', url: 'fdsfds', type: 'x' } ],
[ { name: '123', url: 'fdsfds', type: 'y' },
{ name: '123', url: 'fdsfds', type: 'y' } ],
[ { name: '123', url: 'fdsfds', type: 'z' },
{ name: '123', url: 'fdsfds', type: 'z' } ] ]
~$
用 tableman 的写法:
var tableman = require('tableman');
var _ = require('lodash');
var source = [
[{"name":"123","url":"fdsfds","type":"x"},{"name":"123","url":"fdsfds","type":"y"},{"name":"123","url":"fdsfds","type":"z"}],
[{"name":"123","url":"fdsfds","type":"x"},{"name":"123","url":"fdsfds","type":"y"}]
];
source = _.flatten(source);
var result = [];
tableman.group(source, {
by: 'type',
action: function (rows) {
result.push(rows);
}});
console.log('%j', result);
我没看之前的答案,有点长 :P 如果不想用其他的library,可以这样写(用reduce这些方法会更简单)
var arr = [...] // 你的arr
var obj = {}; // 用来做onlyone
var result = [], tobj;
for ( var i = 0; i < arr.length; i++ ) {
tobj = arr[i];
obj[tobj.type] = obj[tobj.type] || [];
obj[tobj.type].push(tobj[i]);
}
for ( var name in obj ) {
result.push(obj[name]);
}
//没有跑过,可能会有一些问题 :P
看看 lodash 一块钱卖不了吃亏买不了上当