Reputation: 3543
I have an object like this:
const phrases = {
1:{
sentenceID: 1,
reference: "some phrase",
},
2:{
sentenceID: 2,
reference: "It was in",
},
3:{
sentenceID: 2,
reference: "Grand Pabbie's visions",
},
4:{
sentenceID: 2,
reference: "visions",
},
5:{
sentenceID: 3,
reference: "another phrase of a sentence",
},
6:{
sentenceID: 3,
reference: "and this is one more phrase",
},
};
And I want to create an array of objects in which each object in the new array is based on same sentenceID
s...
In other words I want the objects in the new array with same sentenceID
s.
So the desired result would be this:
result = [
{
1: {
sentenceID: 1,
reference: "some phrase",
},
},
{
2: {
sentenceID: 2,
reference: "It was in",
},
3: {
sentenceID: 2,
reference: "Grand Pabbie's visions",
},
4: {
sentenceID: 2,
reference: "visions",
},
},
{
5: {
sentenceID: 3,
reference: "another phrase of a sentence",
},
6: {
sentenceID: 3,
reference: "and this is one more phrase",
},
}
]
Upvotes: 0
Views: 57
Reputation: 4217
Using set:
let phrases={1:{sentenceID:1,reference:"some phrase"},2:{sentenceID:2,reference:"It was in"},3:{sentenceID:2,reference:"Grand Pabbie's visions"},4:{sentenceID:2,reference:"visions"},5:{sentenceID:3,reference:"another phrase of a sentence"},6:{sentenceID:3,reference:"and this is one more phrase"}};
let entries = Object.entries(phrases)
let result = [... new Set(entries.map(([k,v]) => v.sentenceID))]
.map(e => Object.fromEntries(entries.filter( ([k,v]) => v.sentenceID === e )))
console.log(result)
Upvotes: 1
Reputation: 28414
You can use .reduce
to loop over the entries
of phrases
and group the items by the sentenceID
:
const phrases = {
1: { sentenceID: 1, reference: "some phrase" },
2: { sentenceID: 2, reference: "It was in" },
3: { sentenceID: 2, reference: "Grand Pabbie's visions" },
4: { sentenceID: 2, reference: "visions" },
5: { sentenceID: 3, reference: "another phrase of a sentence" },
6: { sentenceID: 3, reference: "and this is one more phrase" },
};
// iterate over the entries
let res = Object.entries(phrases).reduce((acc,item) => {
// get the key and value of the current entry
const [key, value] = item;
// get the attribute we're grouping by
const { sentenceID } = value;
// check if acc already has this sentenceID
const prev = acc[sentenceID];
if(!prev) acc[sentenceID] = {};
// update the subcategory
acc[sentenceID][key] = value;
return acc;
}, {});
// get subcategories grouped by sentence ID
res = Object.values(res);
console.log(res);
Upvotes: 2
Reputation: 386560
Simply group by sentenceID
.
const
phrases = { 1: { sentenceID: 1, reference: "some phrase" }, 2: { sentenceID: 2, reference: "It was in" }, 3: { sentenceID: 2, reference: "Grand Pabbie's visions" }, 4: { sentenceID: 2, reference: "visions" }, 5: { sentenceID: 3, reference: "another phrase of a sentence" }, 6: { sentenceID: 3, reference: "and this is one more phrase" } },
result = Object.values(Object.entries(phrases).reduce((r, [k, o]) => {
r[o.sentenceID] ??= {};
r[o.sentenceID][k] = o;
return r;
}, {}));
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 3