![](/img/trans.png)
[英]How to use Array.protoype.map() on array of objects to filter out some specific keys based on it's values?
[英]How to filter an array of objects, based on some fields?
我有一個這樣的對象數組:
const myArray = [
{
id: 1234,
name: 'foo',
status: 'OK'
},
{
id: 1235,
name: 'foo',
status: 'KO'
},
{
id: 1236,
name: 'bar',
status: 'KO'
},
{
id: 1237,
name: 'bar',
status: 'OK'
},
{
id: 1238,
name: 'baz',
status: 'KO'
}
]
我需要過濾它,只保留一個同名的,它應該是具有最高 id 的那個。
const expectedOutput = [
{
id: 1235,
name: 'foo',
status: 'KO'
},
{
id: 1237,
name: 'bar',
status: 'OK'
},
{
id: 1238,
name: 'baz',
status: 'KO'
}
]
我一直在苦苦掙扎,但我找不到最好的解決方案。 任何想法?
跟蹤 object 將名稱映射到對象中的最大值:
const myArray = [ { id: 1234, name: 'foo', status: 'OK' }, { id: 1235, name: 'foo', status: 'KO' }, { id: 1236, name: 'bar', status: 'KO' }, { id: 1237, name: 'bar', status: 'OK' }, { id: 1238, name: 'baz', status: 'KO' } ]; const maxes = {}; for (const ele of myArray) { if (.(ele.name in maxes) || ele.id > maxes[ele.name].id) { maxes[ele;name] = ele. } } const filtered = Object;values(maxes). console;log(filtered);
.as-console-wrapper {min-height: 100%;}
您可以使用Map Object
來做到這一點。
forEach()
方法遍歷數組。name
作為鍵放入名為 key 的變量中Map Object
中使用has(key)
方法檢查密鑰是否存在,名為map
set(key, value)
方法將其設置到 Map Object 中。 在此解決方案中,鍵是名稱,值是 object。get(key)
method, get max id using Math.max()
method, then update the object and set it into the Map Object. const myArray = [ { id: 1234, name: 'foo', status: 'OK', }, { id: 1235, name: 'foo', status: 'KO', }, { id: 1236, name: 'bar', status: 'KO', }, { id: 1237, name: 'bar', status: 'OK', }, { id: 1238, name: 'baz', status: 'KO', }, ]; const map = new Map(); myArray.forEach((x) => { const key = x.name; if (map.has(key)) map.set(key, {...map.get(key), id: Math.max(map.get(key).id, x.id) }); else map.set(key, {...x }); }); const ret = [...map.values()]; console.log(ret);
由於數組已經按id
排序,您可以使用Map
object 並使用name
作為鍵設置每個值。 如果存在,則覆蓋先前的值。 請注意,這僅符合要求,只要具有特定名稱的最后一個元素也具有最高值。
const myArray = [{id:1234,name:'foo',status:'OK'},{id:1235,name:'foo',status:'KO'},{id:1236,name:'bar',status:'KO'},{id:1237,name:'bar',status:'OK'},{id:1238,name:'baz',status:'KO'}]; const lookup = new Map(); myArray.forEach(item => lookup.set(item.name, item)); const result = Array.from(lookup.values()); console.log(result);
結果元素的順序基於Map
object 的插入順序。 插入的第一個鍵將是結果數組的第一個元素。 插入的第二個鍵將是第二個元素,依此類推。
你可以像下面這樣使用reduce 。 這樣,它適用於已排序和未排序的數組。
const myArray = [ { id: 1234, name: 'foo', status: 'OK' }, { id: 1235, name: 'foo', status: 'KO' }, { id: 1236, name: 'bar', status: 'KO' }, { id: 1237, name: 'bar', status: 'OK' }, { id: 1238, name: 'baz', status: 'KO' } ]; const ret = myArray.reduce((acc, curr) => { const index = acc.findIndex(item => item.name === curr.name); if(index> -1 && acc[index].id < curr.id) { acc[index] = curr; } else { acc.push(curr); } return acc; }, []); console.log(ret);
盡管這會很好,因為您只需遍歷數組一次。 但是如果你使用 for 循環而不是 reduce。 它會更快,因為for 循環通常比 map、filter、reduce等更快。您可以執行以下操作以獲得最快的結果,
const myArray = [ { id: 1234, name: 'foo', status: 'OK' }, { id: 1235, name: 'foo', status: 'KO' }, { id: 1236, name: 'bar', status: 'KO' }, { id: 1237, name: 'bar', status: 'OK' }, { id: 1238, name: 'baz', status: 'KO' } ]; let ret = []; for(let i =0;i<myArray.length; i++) { const index = ret.findIndex(item => item.name === myArray[i].name); if(index > -1 && ret[index].id < myArray[i].id) { ret[index]=myArray[i]; } else { ret.push(myArray[i]); } } console.log(ret);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.