user13849624
user13849624

Reputation:

Removing elements in a complex object

I have a complex object like this: inside each property of the Sentences object we have an array named extensions, I want to remove objects inside it...

// Sentences defined for the whole course
const Sentences = {

    /******** ---Sentence Start--- ********/
    1: {

        type: 'sentence-chunk',
        duration: { start: 32.281, end: 34.608 },
        difficulty: 2,

        clipSentence: {

            threshold: 40,
            reference: "Your Majesty,* they're* ready.",
            expect: "expects_sentences"

        },

        extensions: [

            {
                threshold: 41,
                reference: "reference_sentence",
                expect: "expects_sentences"
            },

            {
                threshold: 42,
                reference: "reference_sentence",
                expect: "expects_sentences"
            },

            {
                threshold: 43,
                reference: "reference_sentence",
                expect: "expects_sentences"
            },
        ],
    }, 
    /******** ---Sentence End--- ********/

    /******** ---Sentence Start--- ********/
    2: {

        type: 'sentence-chunk',
        duration: { start: 32.281, end: 34.608 },
        difficulty: 2,

        clipSentence: {

            threshold: 40,
            reference: "Your Majesty,* they're* ready.",
            expect: "expects_sentences"

        },

        extensions: [

            {
                threshold: 41,
                reference: "Your Highness,* they're* ready.",
                expect: "expects_sentences"
            },

            {
                threshold: 42,
                reference: "Your Majesty,* the guests are* ready.",
                expect: "expects_sentences"
            },

            {
                threshold: 43,
                reference: "Your Majesty,* they're* ready.",
                expect: "expects_sentences"
            },
        ],
    }, 
    /******** ---Sentence End--- ********/

}

modifySentences();
  console.log(Sentences);

  function modifySentences() {
    for (const [key, sentence] of Object.entries(Sentences)) {
        for(let i = 0; i < sentence.extensions.length; i++) {
           if(sentence.extensions[i].reference === 'reference_sentence') {
            console.log(i)
            sentence.extensions.splice(i, 1);
          }
        }
    }
  }

I want to remove objects inside extensions array if the reference property is equal to reference_sentence.

But the code I wrote is not working fine and one of the objects remain untouched!

How can I fix this?

Upvotes: 0

Views: 214

Answers (3)

phi-rakib
phi-rakib

Reputation: 3302

Simple array map and filter method could do your job.

const Sentences = {
  1: {
    type: 'sentence-chunk',
    duration: { start: 32.281, end: 34.608 },
    difficulty: 2,

    clipSentence: {
      threshold: 40,
      reference: "Your Majesty,* they're* ready.",
      expect: 'expects_sentences',
    },

    extensions: [
      {
        threshold: 41,
        reference: 'reference_sentence',
        expect: 'expects_sentences',
      },

      {
        threshold: 42,
        reference: 'reference_sentence',
        expect: 'expects_sentences',
      },

      {
        threshold: 43,
        reference: 'reference_sentence',
        expect: 'expects_sentences',
      },
    ],
  },
  2: {
    type: 'sentence-chunk',
    duration: { start: 32.281, end: 34.608 },
    difficulty: 2,

    clipSentence: {
      threshold: 40,
      reference: "Your Majesty,* they're* ready.",
      expect: 'expects_sentences',
    },

    extensions: [
      {
        threshold: 41,
        reference: "Your Highness,* they're* ready.",
        expect: 'expects_sentences',
      },

      {
        threshold: 42,
        reference: 'Your Majesty,* the guests are* ready.',
        expect: 'expects_sentences',
      },

      {
        threshold: 43,
        reference: "Your Majesty,* they're* ready.",
        expect: 'expects_sentences',
      },
    ],
  },
};

const ret = Object.entries(Sentences).map(([, sentence]) => {
  sentence.extensions = sentence.extensions.filter(
    (x) => x.reference !== 'reference_sentence'
  );
  return sentence;
});
console.log(ret);

Upvotes: 2

user2953241
user2953241

Reputation: 365

You could try

function modifySentences() {
    for (const key of Object.keys(Sentences)) {
      if (Sentences.hasOwnProperty(key)) {
         Sentences[key].extensions = Sentences[key].extensions.filter(ext => ext.reference !== "reference_sentence")
      }
    }
}

Thie hasOwnProperty() check is needed, to only iterate over those properties you defined.

To see what filter takes as arguments check out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Upvotes: 0

Nikita Chayka
Nikita Chayka

Reputation: 2137

When you are splicing - you removing object from array, meaning you changing it's length, and indexes of remaining elements.

So what you should do is when you splicing, you just "go one iteration back" by simply decrementing your i:

// Sentences defined for the whole course
const Sentences = {

    /******** ---Sentence Start--- ********/
    1: {

        type: 'sentence-chunk',
        duration: { start: 32.281, end: 34.608 },
        difficulty: 2,

        clipSentence: {

            threshold: 40,
            reference: "Your Majesty,* they're* ready.",
            expect: "expects_sentences"

        },

        extensions: [

            {
                threshold: 41,
                reference: "reference_sentence",
                expect: "expects_sentences"
            },

            {
                threshold: 42,
                reference: "reference_sentence",
                expect: "expects_sentences"
            },

            {
                threshold: 43,
                reference: "reference_sentence",
                expect: "expects_sentences"
            },
        ],
    }, 
    /******** ---Sentence End--- ********/

    /******** ---Sentence Start--- ********/
    2: {

        type: 'sentence-chunk',
        duration: { start: 32.281, end: 34.608 },
        difficulty: 2,

        clipSentence: {

            threshold: 40,
            reference: "Your Majesty,* they're* ready.",
            expect: "expects_sentences"

        },

        extensions: [

            {
                threshold: 41,
                reference: "Your Highness,* they're* ready.",
                expect: "expects_sentences"
            },

            {
                threshold: 42,
                reference: "Your Majesty,* the guests are* ready.",
                expect: "expects_sentences"
            },

            {
                threshold: 43,
                reference: "Your Majesty,* they're* ready.",
                expect: "expects_sentences"
            },
        ],
    }, 
    /******** ---Sentence End--- ********/

}

modifySentences();
  console.log(Sentences);

  function modifySentences() {
    for (const [key, sentence] of Object.entries(Sentences)) {
        for(let i = 0; i < sentence.extensions.length; i++) {
           if(sentence.extensions[i].reference === 'reference_sentence') {
            console.log(i)
            sentence.extensions.splice(i, 1);
            --i; //here it is, you resetting index back, so you could check next object
          }
        }
    }
  }

Upvotes: 3

Related Questions