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

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

當(dāng)我更改另一個(gè)數(shù)組中的值時(shí),數(shù)組中的值會(huì)更改嗎?

當(dāng)我更改另一個(gè)數(shù)組中的值時(shí),數(shù)組中的值會(huì)更改嗎?

明月笑刀無情 2021-12-12 09:32:07
我有兩個(gè)數(shù)組的問題。每當(dāng)我使用下面顯示的代碼更改一個(gè)數(shù)組中的值時(shí),另一個(gè)數(shù)組也會(huì)得到相同的更改,這不是預(yù)期的。如果我將下面的代碼復(fù)制并粘貼到瀏覽器的 javascript 控制臺(tái)中,我會(huì)遇到在調(diào)用 ConvertDataArrayToLocationArray(dataArray) 后更改 originalArray 的問題let originalArray = [  {    "date": "2018-11-16",    "type": "Entertainment",    "location": "Oslo",    "amount": 1024  },  {    "date": "2018-11-16",    "type": "Food",    "location": "Oslo",    "amount": 170  },  {    "date": "2018-11-17",    "type": "Food",    "location": "Fredrikstad",    "amount": 99  },  {    "date": "2018-11-18",    "type": "Food",    "location": "Halden",    "amount": 29  },  {    "date": "2018-11-19",    "type": "Entertainment",    "location": "Oslo",    "amount": 34  },  {    "date": "2018-11-20",    "type": "Entertainment",    "location": "Oslo",    "amount": 15  },  {    "date": "2018-11-20",    "type": "Food",    "location": "Fredrikstad",    "amount": 80  },  {    "date": "2018-11-23",    "type": "Transportation",    "location": "Stavanger",    "amount": 95  },  {    "date": "2018-11-28",    "type": "Entertainment",    "location": "Oslo",    "amount": 1024  },  {    "date": "2018-11-29",    "type": "Food",    "location": "Oslo",    "amount": 117.39  },  {    "date": "2018-11-30",    "type": "Transportation",    "location": "Fredrikstad",    "amount": 29  },  {    "date": "2018-12-2",    "type": "Transportation",    "location": "Stavanger",    "amount": 184  },  {    "date": "2018-12-3",    "type": "Entertainment",    "location": "Oslo",    "amount": 34  },  {    "date": "2018-12-4",    "type": "Food",    "location": "Oslo",    "amount": 162  },  {    "date": "2018-12-4",    "type": "Food",    "location": "Fredrikstad",    "amount": 231  }];我的例外結(jié)果是稱為 originalArray 的變量保持不變,我從 ConvertDataArrayToLocationArray(dataArray) 的返回值中得到一個(gè)新數(shù)組。
查看完整描述

2 回答

?
偶然的你

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

當(dāng)您將項(xiàng)目插入到 中時(shí)newArray,您正在傳遞對(duì)該對(duì)象的引用。

因此,對(duì)新復(fù)制數(shù)組newArray中的項(xiàng)目所做的任何更改都會(huì)反映在原始數(shù)組中,反之亦然。

為了防止這種情況,不要傳遞引用,而是傳遞對(duì)象的副本。

newArray.push({...dataArray[i]});

我正在使用 ES6擴(kuò)展語法進(jìn)行復(fù)制。我們還有Object.assign()方法和其他幾種克隆對(duì)象的方法。

對(duì)于您的數(shù)據(jù),這些就足夠了,因?yàn)樗袑傩远际腔?。如果有你必須使用的?duì)象屬性JSON.parse(JSON.stringify(dataArray[i]))或其他方法。


查看完整回答
反對(duì) 回復(fù) 2021-12-12
?
一只甜甜圈

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

讓使用深副本: const dataArray = JSON.parse(JSON.stringify(arr));:


let originalArray = [

  {

    date: "2018-11-16",

    type: "Entertainment",

    location: "Oslo",

    amount: 1024

  },

  {

    date: "2018-11-16",

    type: "Food",

    location: "Oslo",

    amount: 170

  },

  {

    date: "2018-11-17",

    type: "Food",

    location: "Fredrikstad",

    amount: 99

  },

  {

    date: "2018-11-18",

    type: "Food",

    location: "Halden",

    amount: 29

  },

  {

    date: "2018-11-19",

    type: "Entertainment",

    location: "Oslo",

    amount: 34

  },

  {

    date: "2018-11-20",

    type: "Entertainment",

    location: "Oslo",

    amount: 15

  },

  {

    date: "2018-11-20",

    type: "Food",

    location: "Fredrikstad",

    amount: 80

  },

  {

    date: "2018-11-23",

    type: "Transportation",

    location: "Stavanger",

    amount: 95

  },

  {

    date: "2018-11-28",

    type: "Entertainment",

    location: "Oslo",

    amount: 1024

  },

  {

    date: "2018-11-29",

    type: "Food",

    location: "Oslo",

    amount: 117.39

  },

  {

    date: "2018-11-30",

    type: "Transportation",

    location: "Fredrikstad",

    amount: 29

  },

  {

    date: "2018-12-2",

    type: "Transportation",

    location: "Stavanger",

    amount: 184

  },

  {

    date: "2018-12-3",

    type: "Entertainment",

    location: "Oslo",

    amount: 34

  },

  {

    date: "2018-12-4",

    type: "Food",

    location: "Oslo",

    amount: 162

  },

  {

    date: "2018-12-4",

    type: "Food",

    location: "Fredrikstad",

    amount: 231

  }

];


function ConvertDataArrayToLocationArray(arr) {

  const dataArray = JSON.parse(JSON.stringify(arr));

  let newArray = [];


  for (let i = 0; i < dataArray.length; i++) {

    let existed = false;


    for (let j = 0; j < newArray.length; j++) {

      if (dataArray[i].location === newArray[j].location) {

        newArray[j].amount = newArray[j].amount + 10;


        existed = true;

      }

    }


    if (!existed) {

      newArray.push(dataArray[i]);

    }

  }


  return newArray;

}


let a = ConvertDataArrayToLocationArray(originalArray);


console.log(originalArray[0]);

console.log(a[0]);


查看完整回答
反對(duì) 回復(fù) 2021-12-12
  • 2 回答
  • 0 關(guān)注
  • 274 瀏覽
慕課專欄
更多

添加回答

舉報(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)