[英]Filtering an array of objects that contain arrays
这是我所拥有的数组的较小版本,但它具有相同的结构
使用下面的const arr,我想创建2个具有唯一值的新数组,这些数组按升序排序
const arr = [{ tags: ['f', 'b', 'd'], weight: 7, something: 'sdfsdf' }, { tags: ['a', 'b', 'c', 'd', 'e'], weight: 6, something: 'frddd' }, { tags: ['f', 'c', 'e', 'a'], weight: 7, something: 'ththh' }, { tags: ['a', 'c', 'g', 'e'], weight: 5, something: 'ghjghj' } ]; const finalTags = []; const finalWeight = []; // TODO: find a better way to do this arr.forEach(v => { if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight); v.tags.forEach(val => { if (finalTags.indexOf(val) === -1) finalTags.push(val); }); }); // Ascending order finalTags.sort(); finalWeight.sort();
我上面有什么作品,但看起来有点乱,如果有更好/更整洁的方式这样做就会徘徊
您可以将Array.prototype.reduce()与Set结合使用,以获取具有已排序数组的对象{tags: [], weights: []}
:
const arr = [{tags: ['f', 'b', 'd'],weight: 7,something: 'sdfsdf'},{tags: ['a', 'b', 'c', 'd', 'e'],weight: 6,something: 'frddd'},{tags: ['f', 'c', 'e', 'a'],weight: 7,something: 'ththh'},{tags: ['a', 'c', 'g', 'e'],weight: 5,something: 'ghjghj'}]; const obj = arr.reduce((a, {tags, weight}) => { a.tags = [...new Set(a.tags.concat(tags))]; a.weights = [...new Set(a.weights.concat(weight))]; return a; }, {tags: [], weights: []}); // final result i want console.log('finalTags:', obj.tags.sort()); // ['a', 'b', 'c', 'd', 'e', 'f', 'g']; console.log('finalWeight:', obj.weights.sort()); // [5, 6, 7];
.as-console-wrapper { max-height: 100% !important; top: 0; }
一种解决方案是使用Array.reduce()创建两个集合,一个带有tags
,另一个带有权weights
。 在此之后,您可以将sets
转换为arrays
并在它们上使用Array.sort() :
const arr = [ { tags: ['f', 'b', 'd'], weight: 7, something: 'sdfsdf' }, { tags: ['a', 'b', 'c', 'd', 'e'], weight: 6, something: 'frddd' }, { tags: ['f', 'c', 'e', 'a'], weight: 7, something: 'ththh' }, { tags: ['a', 'c', 'g', 'e'], weight: 5, something: 'ghjghj' } ]; let res = arr.reduce((acc, {tags, weight}) => { acc.tags = new Set([...acc.tags, ...tags]); acc.weights.add(weight); return acc; }, {tags: new Set(), weights: new Set()}); let sortedWeigths = [...res.weights].sort(); let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b)); console.log("weights: ", sortedWeigths, "tags: ", sortedTags);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
您可以使用以下代码。 这基本上分裂arr
成finalTags
和finalWeights
。
.flat()
平数组( [1, [2, [3]]]
将变为[1, 2, 3]
)
finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort();
删除重复项。
const arr = [{ tags: ['f', 'b', 'd'], weight: 7, something: 'sdfsdf' }, { tags: ['a', 'b', 'c', 'd', 'e'], weight: 6, something: 'frddd' }, { tags: ['f', 'c', 'e', 'a'], weight: 7, something: 'ththh' }, { tags: ['a', 'c', 'g', 'e'], weight: 5, something: 'ghjghj' } ]; let finalTags = arr.map(e => e.tags); finalTags = finalTags.flat(); finalTags = finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort(); let finalWeight = arr.map(e => e.weight); finalWeight = finalWeight.filter((item, index) => finalWeight.indexOf(item) >= index).sort(); console.log(finalTags); console.log(finalWeight);
资料来源:
删除重复项: https : //gomakethings.com/removing-duplicates-from-an-array-with-vanilla-javascript/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.