![](/img/trans.png)
[英]How to merge objects of an array having same property and different values using typescript?
[英]How to find objects with the same property values in an Array of Objects in typescript?
我有一個對象數組
var myArray = [
{id: 1, name: 'Foo Bar', email: 'foo@bar.com'},
{id: 2, name: 'Bar Foo', email: 'bar@foo.com'},
{id: 3, name: 'Joe Ocean', email: 'joe@ocean.com'},
{id: 3, name: 'Jenny Block', email: 'foo@bar.com'},
];
我期待以下輸出:
commonIdsObjects = [
{id: 3, name: 'Joe Ocean', email: 'joe@ocean.com'},
{id: 3, name: 'Jenny Block', email: 'foo@bar.com'},
]
我假設您希望輸出是包含所有重復條目的單個數組,即使其中一些條目具有不同的 ID。 例如,如果您已將{id: 2, name: 'Fishy Joe', email: 'com@foo.bar'}
到myArray
,則生成的commonIdsObjects
將是一個包含四個項目的數組:兩個用於id: 2
和兩個對於id: 3
。 如果這不是您想要的,那么您應該注意准確指定預期的行為。
無論如何,假設您有一個對應於myArray
元素的類型,如下所示:
type Elem = typeof myArray[number];
並假設您的目標運行時可以訪問Object.values()
和Array.prototype.flat()
方法,那么您可以編寫
const commonIdsObjects = Object.values(
myArray.reduce<{ [k: number]: Elem[] }>(
(a, v) => ((a[v.id] || (a[v.id] = [])).push(v), a), {}
)
).filter(c => c.length > 1).flat(1);
我們正在做的是使用myArray.reduce()
構建一個對象,其鍵對應於元素的id
值,其值是具有這些id
的元素數組。 我們把這個對象轉換成一個元素數組的數組,只保留那些長度大於一的(即任何一個有多個元素與之對應的id),並扁平化成一個數組。
這將產生所需的結果:
console.log(JSON.stringify(commonIdsObjects));
// [{"id":3,"name":"Joe Ocean","email":"joe@ocean.com"},
// {"id":3,"name":"Jenny Block","email":"foo@bar.com"}]
如果您無權訪問Object.values()
和[].flat()
您可以使用Object.keys()
和[].reduce()
代替:
type ElemsById = { [k: string]: Elem[] }
const commonIdsObjects2 = ((o: ElemsById) => Object.keys(o).map(k => o[k]))(
myArray.reduce<ElemsById>(
(a, v) => ((a[v.id] || (a[v.id] = [])).push(v), a), {}))
.filter(c => c.length > 1).reduce<Elem[]>((a, v) => (a.push(...v), a), []);
console.log(JSON.stringify(commonIdsObjects2)); // same
這本質上是相同的算法。 或者您可以使用各種for
循環以純命令式編程方式執行此算法:
const elemsById: ElemsById = {};
for (let v of myArray) {
if (!elemsById[v.id]) {
elemsById[v.id] = []
}
elemsById[v.id].push(v);
}
const commonIdsObjects3: Elem[] = []
for (let k in elemsById) {
if (elemsById[k].length <= 1) {
continue;
}
for (let v of elemsById[k]) {
commonIdsObjects3.push(v);
}
}
console.log(JSON.stringify(commonIdsObjects3)); // same
好的,希望有幫助; 祝你好運!
var myArray = [
{ id: 1, name: "Foo Bar", email: "foo@bar.com" },
{ id: 2, name: "Bar Foo", email: "bar@foo.com" },
{ id: 3, name: "Joe Ocean", email: "joe@ocean.com" },
{ id: 3, name: "Jenny Block", email: "foo@bar.com" }];
const commonIdsObjects = myArray.filter(x => x.id === 3);
console.log(commonIdsObjects);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.