第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

刪除復(fù)雜對(duì)象中的元素

刪除復(fù)雜對(duì)象中的元素

慕森王 2023-07-06 11:09:28
我有一個(gè)像這樣的復(fù)雜對(duì)象:在 Sentences 對(duì)象的每個(gè)屬性中,我們有一個(gè)名為 extensions 的數(shù)組,我想刪除其中的對(duì)象...// 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,我想刪除擴(kuò)展數(shù)組內(nèi)的對(duì)象。但我編寫(xiě)的代碼無(wú)法正常工作,并且其中一個(gè)對(duì)象保持不變!我怎樣才能解決這個(gè)問(wèn)題?
查看完整描述

3 回答

?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

當(dāng)您進(jìn)行拼接時(shí) - 您從數(shù)組中刪除對(duì)象,這意味著您更改了它的長(zhǎng)度以及剩余元素的索引。


所以你應(yīng)該做的是,當(dāng)你拼接時(shí),你只需通過(guò)簡(jiǎn)單地遞減你的值來(lái)“返回一次迭代” 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

          }

        }

    }

  }


查看完整回答
反對(duì) 回復(fù) 2023-07-06
?
ITMISS

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊

簡(jiǎn)單的數(shù)組映射和過(guò)濾方法就可以完成您的工作。


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


查看完整回答
反對(duì) 回復(fù) 2023-07-06
?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

你可以嘗試


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() 檢查,以便僅迭代您定義的那些屬性。

查看完整回答
反對(duì) 回復(fù) 2023-07-06
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)