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

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

如何使用循環(huán)索引獲取對(duì)象中的值

如何使用循環(huán)索引獲取對(duì)象中的值

偶然的你 2023-03-24 17:11:39
我有一個(gè)提交多個(gè)數(shù)據(jù)的 javascript 表單集,如果我得到所有的發(fā)布數(shù)據(jù),我有類似下面的內(nèi)容firstname1: "John",lastname1: "Doe",firstname2: "Mary",lastname2: "Allinson",firstname3: "David"lastname3: "Mark",eventDesctiption: "Lorem Ipsum...",eventDate: "Lorem Ipsum..."在本例中,我有一個(gè)隱藏字段,其中包含提交的姓名數(shù)量;它的 3. 我希望能夠循環(huán)遍歷名稱并將它們放入對(duì)象數(shù)組中,然后再發(fā)布到 API,我希望能夠?qū)崿F(xiàn)以下目標(biāo){eventDesctiption: "Lorem Ipsum...",eventDate: "Lorem Ipsum...",people: [    {firstname: "John", lastname: "Doe"},    {firstname: "Mary", lastname: "Allinson"},    {firstname: "David", lastname: "Mark"},    ]}我嘗試了下面的方法,但它似乎將索引與值連接起來,這不是我想要的peopleArray = new Array();for(var i=1; i<=no_of_ben; i++){            var peopleObject = {};                        peopleObject.firstname = data.firstname + 'i';peopleObject.lastname = data.lastname + 'i';            peopleArray.push(peopleObject);        }如何在不連接索引的情況下執(zhí)行此操作
查看完整描述

3 回答

?
一只斗牛犬

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

const input = {

  firstname1: "John",

  lastname1: "Doe",

  firstname2: "Mary",

  lastname2: "Allinson",

  firstname3: "David",

  lastname3: "Mark",

  eventDescription: "Lorem Ipsum...",

  eventDate: "Lorem Ipsum..."

};


const output = {

  eventDescription: input.eventDescription,

  eventDate: input.eventDate,

  people: []

};


const peopleCount = 3; // You said you have this one somewhere

for (let i = 1; i <= peopleCount; i++) {

  const onePerson = {

    firstname: input['firstname' + i],

    lastname: input['lastname' + i]

  };

  output.people.push(onePerson);

}


console.log(output);


查看完整回答
反對(duì) 回復(fù) 2023-03-24
?
喵喔喔

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

嘗試這個(gè)。應(yīng)該是工作


peopleArray = new Array();

data = {

  firstname1: 'king', lastname1: 'James',

  firstname2: '2ndName', lastname2: '2ndLast',

  firstname3: 'alice', lastname3: 'bambam'

};


for(var i=1; i<=3; i++){

  var x = 'firstname';

  var y = 'lastname';

  var peopleObject = {};

  x = x + i;

  y = y + i;

  peopleObject.firstname = data[x];

  peopleObject.lastname = data[y];

  peopleArray.push(peopleObject);

}


console.log(peopleArray);


查看完整回答
反對(duì) 回復(fù) 2023-03-24
?
慕雪6442864

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

檢查這是否有效..


peopleArray = new Array();

for(var i=1; i<=no_of_ben; i++){

        var peopleObject = {};            

        peopleObject.firstname = data['firstname' + 'i'];

        peopleObject.lastname = data['lastname' + 'i'];

        peopleArray.push(peopleObject);

    }

將data.firstname + 'i'替換為data['firstname' + 'i']


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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