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 sentenceIDs...
In other words I want the objects in the new array with same sentenceIDs.
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",
},
}
]
asked Dec 10, 2020 at 20:58
Sara Ree
3,5541 gold badge20 silver badges74 bronze badges
3 Answers 3
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; }
answered Dec 10, 2020 at 21:07
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Ben Stephens
If anyone else hasn't seen ??= before: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
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);
answered Dec 10, 2020 at 21:09
Majed Badawi
28.5k4 gold badges30 silver badges56 bronze badges
Comments
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)
answered Dec 10, 2020 at 21:30
Alan Omar
4,2971 gold badge13 silver badges26 bronze badges
Comments
lang-js
1in an object of its own, but2,3,4are collected in a single object?sentenceIDsentenceIDas the key, and the value is the new grouped objects you want to create in the result.