我想要一个对象数组:
var objArr = [
{id:1, name:'test', seenby:'user1, user2, user3'},
{id:2, name:'test1', seenby:'user3, user4'},
{id:3, name:'test2', seenby:'user1, user3'},
{id:4, name:'test3', seenby:'user2'}
];
并返回所有“seenby”用户的不同数组:
var seenByArr = ['user1', 'user2', 'user3', 'user4']
我无法弄清楚如何有效地(可能的最小限度的线数)将其变成一系列不同的值。请查看我的小提琴作为示例:https://jsfiddle.net/ksumarine/ss3v7dgj/
最佳答案
您可以尝试关注
$.each(list, function(index, value) {
$.each(value.seenby.split(","), function(i, val) {
if (val !== undefined && val.trim() !== "" && arr.indexOf(val.trim()) == -1) {
arr.push(val.trim());
}
});
});
供引用-https://jsfiddle.net/ss3v7dgj/1/