0

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

    }
]
8
  • what goes wrong? please add your code. Commented Dec 10, 2020 at 20:59
  • Nothing ... I just need you again Nina! Commented Dec 10, 2020 at 21:00
  • What's the logic here? Why is 1 in an object of its own, but 2, 3, 4 are collected in a single object? Commented Dec 10, 2020 at 21:01
  • Ah, they're grouped by sentenceID Commented Dec 10, 2020 at 21:02
  • Loop through the original object, creating a new object that uses sentenceID as the key, and the value is the new grouped objects you want to create in the result. Commented Dec 10, 2020 at 21:03

3 Answers 3

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; }

Sign up to request clarification or add additional context in comments.

1 Comment

2

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);

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.