Sara Ree
Sara Ree

Reputation: 3543

Generate array of objects based on object of objects

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",
        },

    }
]

Upvotes: 0

Views: 57

Answers (3)

Alan Omar
Alan Omar

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

Majed Badawi
Majed Badawi

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

Nina Scholz
Nina Scholz

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

Related Questions