[英]get the length of an array with unique values
所以我有一個包含一些對象的數組。 我想將這些對象相互比較。 如果fromId
和 toId
屬性相同,則它是重復的。
例:
const data = [{ fromId: 1, toId: 2, finished: true }, { fromId: 1, toId: 2, finished: false }, { fromId: 5, toId: 9, finished: false }, { fromId: 1, toId: 5, finished: true }, { fromId: 2, toId: 1, finished: false }]; $(document).ready(() => { const duplicates = data.filter(x => data .filter(y => y.fromId == x.fromId && y.toId == x.toId) .length > 1 ); console.log(duplicates); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我想得到這個數據數組的長度,但只有唯一值。 我試圖刪除所有重復的值,並在此處找到解決方案
const data = [1, 2, 3, 4, 5, 2, 2, 6, 7, 8, 2, 9, 2, 2, 2, 2, 2]; $(document).ready(() => { const uniqueValues = data.filter((connection, index) => data.indexOf(connection) == index); console.log(uniqueValues); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
但是在這個解決方案中我只使用indexOf
作為整個對象。 如何比較fromId
和toId
並刪除相同匹配的副本?
從原始數組我將刪除一個
{
fromId: 1,
toId: 2,
finished: false
}
因為fromId
和toId
已存在於另一個對象中。
而不是filter
嵌套在另一個filter
(壞的運行復雜性),因為你只是在尋找所產生的獨特的數組的長度,你可能會改為每結合fromId
和toId
成一個字符串,將這個字符串變成一個Set
(只保留唯一值),然后檢查Set
的大小:
const data=[{fromId:1,toId:2,finished:!0},{fromId:1,toId:2,finished:!1},{fromId:5,toId:9,finished:!1},{fromId:1,toId:5,finished:!0},{fromId:2,toId:1,finished:!1}]; const idSet = new Set(data.map(({ fromId, toId }) => fromId + '_' + toId)); console.log(idSet.size);
您可以使用Array.reduce()
過濾具有唯一對象的數組,然后將length值與唯一數組一起獲取:
const data = [{ fromId: 1, toId: 2, finished: true }, { fromId: 1, toId: 2, finished: false }, { fromId: 5, toId: 9, finished: false }, { fromId: 1, toId: 5, finished: true }, { fromId: 2, toId: 1, finished: false }]; $(document).ready(() => { var uniqueArray = data.reduce((acc, obj)=>{ var existObj = acc.find(item => item.fromId === obj.fromId && item.toId === obj.toId); if(existObj){ return acc; } acc.push(obj); return acc; },[]); console.log('unique array ', uniqueArray); console.log('length of unique object ', uniqueArray.length); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
你可以創建一個新數組 ,只推送一次,檢查它們是否已經存在或者不使用jQuery中的inArray函數:
const data = [{ fromId: 1, toId: 2, finished: true }, { fromId: 1, toId: 2, finished: false }, { fromId: 5, toId: 9, finished: false }, { fromId: 1, toId: 5, finished: true }, { fromId: 2, toId: 1, finished: false }]; $(document).ready(() => { var filtered = []; $.each(data, function(i, item){ if($.inArray(item, filtered) === -1){ filtered.push(item); } }); console.log(filtered.length); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
jQuery inArray()
雖然對於您的場景,CertainPerformance的答案可能更好,但是不比較項標識的indexOf
替代方法是findIndex
。 但是,這只是ES6 +,但大多數現代瀏覽器都實現它(抱歉,沒有IE11)。
資料來源: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
有一個簡單的forEach - 這是簡單的JS而不是ES5 / 6或jQuery
const data = [{fromId: 1,toId: 2,finished: true},{fromId: 1,toId: 2,finished: false}, {fromId: 5,toId: 9,finished: false}, {fromId: 1,toId: 5,finished: true}, {fromId: 2, toId: 1, finished: false }]; var combos = []; var dupes = []; var unique = []; data.forEach(function(item){ var combo = ""+item.fromId+"."+item.toId; if (combos.indexOf(combo) == -1) { combos.push(combo); unique.push(item); } else { dupes.push(item); } }); console.log("dupes",dupes); console.log("unique",unique,unique.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
是的,有很多可用的選項...會給你另一個lodash/fp
:
假設你有:
import {flow, map, uniq} from 'lodash/fp'
做這個:
const count = flow(
map(o => o.fromId + '.' + o.toId),
uniq,
)(data).length
console.log('Count: ', count)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.