亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

刪除復雜對象中的元素

刪除復雜對象中的元素

慕森王 2023-07-06 11:09:28
我有一個像這樣的復雜對象:在 Sentences 對象的每個屬性中,我們有一個名為 extensions 的數組,我想刪除其中的對象...// Sentences defined for the whole courseconst 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--- ********/}如果引用屬性等于reference_sentence,我想刪除擴展數組內的對象。但我編寫的代碼無法正常工作,并且其中一個對象保持不變!我怎樣才能解決這個問題?
查看完整描述

3 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

當您進行拼接時 - 您從數組中刪除對象,這意味著您更改了它的長度以及剩余元素的索引。


所以你應該做的是,當你拼接時,你只需通過簡單地遞減你的值來“返回一次迭代” 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

          }

        }

    }

  }


查看完整回答
反對 回復 2023-07-06
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

簡單的數組映射和過濾方法就可以完成您的工作。


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


查看完整回答
反對 回復 2023-07-06
?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

你可以嘗試


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

? ? ? }

? ? }

}

需要 hasOwnProperty() 檢查,以便僅迭代您定義的那些屬性。

查看完整回答
反對 回復 2023-07-06
  • 3 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號