删除复杂对象中的元素

我有一个像这样的复杂对象:在 Sentences 对象的每个属性中,我们有一个名为 extensions 的数组,我想删除其中的对象...


// 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--- ********/


}

如果引用属性等于reference_sentence,我想删除扩展数组内的对象。


但我编写的代码无法正常工作,并且其中一个对象保持不变!


我怎样才能解决这个问题?


慕森王
浏览 99回答 3
3回答

绝地无双

当您进行拼接时 - 您从数组中删除对象,这意味着您更改了它的长度以及剩余元素的索引。所以你应该做的是,当你拼接时,你只需通过简单地递减你的值来“返回一次迭代” i:// Sentences defined for the whole courseconst Sentences = {&nbsp; &nbsp; /******** ---Sentence Start--- ********/&nbsp; &nbsp; 1: {&nbsp; &nbsp; &nbsp; &nbsp; type: 'sentence-chunk',&nbsp; &nbsp; &nbsp; &nbsp; duration: { start: 32.281, end: 34.608 },&nbsp; &nbsp; &nbsp; &nbsp; difficulty: 2,&nbsp; &nbsp; &nbsp; &nbsp; clipSentence: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threshold: 40,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reference: "Your Majesty,* they're* ready.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect: "expects_sentences"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; extensions: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threshold: 41,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reference: "reference_sentence",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect: "expects_sentences"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threshold: 42,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reference: "reference_sentence",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect: "expects_sentences"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threshold: 43,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reference: "reference_sentence",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect: "expects_sentences"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; },&nbsp;&nbsp; &nbsp; /******** ---Sentence End--- ********/&nbsp; &nbsp; /******** ---Sentence Start--- ********/&nbsp; &nbsp; 2: {&nbsp; &nbsp; &nbsp; &nbsp; type: 'sentence-chunk',&nbsp; &nbsp; &nbsp; &nbsp; duration: { start: 32.281, end: 34.608 },&nbsp; &nbsp; &nbsp; &nbsp; difficulty: 2,&nbsp; &nbsp; &nbsp; &nbsp; clipSentence: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threshold: 40,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reference: "Your Majesty,* they're* ready.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect: "expects_sentences"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; extensions: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threshold: 41,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reference: "Your Highness,* they're* ready.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect: "expects_sentences"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threshold: 42,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reference: "Your Majesty,* the guests are* ready.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect: "expects_sentences"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threshold: 43,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reference: "Your Majesty,* they're* ready.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect: "expects_sentences"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; },&nbsp;&nbsp; &nbsp; /******** ---Sentence End--- ********/}modifySentences();&nbsp; console.log(Sentences);&nbsp; function modifySentences() {&nbsp; &nbsp; for (const [key, sentence] of Object.entries(Sentences)) {&nbsp; &nbsp; &nbsp; &nbsp; for(let i = 0; i < sentence.extensions.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(sentence.extensions[i].reference === 'reference_sentence') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sentence.extensions.splice(i, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --i; //here it is, you resetting index back, so you could check next object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }

ITMISS

简单的数组映射和过滤方法就可以完成您的工作。const Sentences = {&nbsp; 1: {&nbsp; &nbsp; type: 'sentence-chunk',&nbsp; &nbsp; duration: { start: 32.281, end: 34.608 },&nbsp; &nbsp; difficulty: 2,&nbsp; &nbsp; clipSentence: {&nbsp; &nbsp; &nbsp; threshold: 40,&nbsp; &nbsp; &nbsp; reference: "Your Majesty,* they're* ready.",&nbsp; &nbsp; &nbsp; expect: 'expects_sentences',&nbsp; &nbsp; },&nbsp; &nbsp; extensions: [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; threshold: 41,&nbsp; &nbsp; &nbsp; &nbsp; reference: 'reference_sentence',&nbsp; &nbsp; &nbsp; &nbsp; expect: 'expects_sentences',&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; threshold: 42,&nbsp; &nbsp; &nbsp; &nbsp; reference: 'reference_sentence',&nbsp; &nbsp; &nbsp; &nbsp; expect: 'expects_sentences',&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; threshold: 43,&nbsp; &nbsp; &nbsp; &nbsp; reference: 'reference_sentence',&nbsp; &nbsp; &nbsp; &nbsp; expect: 'expects_sentences',&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; },&nbsp; 2: {&nbsp; &nbsp; type: 'sentence-chunk',&nbsp; &nbsp; duration: { start: 32.281, end: 34.608 },&nbsp; &nbsp; difficulty: 2,&nbsp; &nbsp; clipSentence: {&nbsp; &nbsp; &nbsp; threshold: 40,&nbsp; &nbsp; &nbsp; reference: "Your Majesty,* they're* ready.",&nbsp; &nbsp; &nbsp; expect: 'expects_sentences',&nbsp; &nbsp; },&nbsp; &nbsp; extensions: [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; threshold: 41,&nbsp; &nbsp; &nbsp; &nbsp; reference: "Your Highness,* they're* ready.",&nbsp; &nbsp; &nbsp; &nbsp; expect: 'expects_sentences',&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; threshold: 42,&nbsp; &nbsp; &nbsp; &nbsp; reference: 'Your Majesty,* the guests are* ready.',&nbsp; &nbsp; &nbsp; &nbsp; expect: 'expects_sentences',&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; threshold: 43,&nbsp; &nbsp; &nbsp; &nbsp; reference: "Your Majesty,* they're* ready.",&nbsp; &nbsp; &nbsp; &nbsp; expect: 'expects_sentences',&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; },};const ret = Object.entries(Sentences).map(([, sentence]) => {&nbsp; sentence.extensions = sentence.extensions.filter(&nbsp; &nbsp; (x) => x.reference !== 'reference_sentence'&nbsp; );&nbsp; return sentence;});console.log(ret);

犯罪嫌疑人X

你可以尝试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() 检查,以便仅迭代您定义的那些属性。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript