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

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

將兩個(gè)嵌套的 json 對(duì)象與 javascript 結(jié)合起來(lái),并用一個(gè)鍵循環(huán)遍歷它

將兩個(gè)嵌套的 json 對(duì)象與 javascript 結(jié)合起來(lái),并用一個(gè)鍵循環(huán)遍歷它

ITMISS 2022-12-18 16:35:49
我有幾個(gè) json 對(duì)象(obj1、obj2...),每個(gè)對(duì)象都取自 .txt 文件:{  "countries": [    {      "Country name": "China",      "Flag": "CN",      "Population": 1395380000,      "undefined": "#688144"    }, ... ]}和{  "countries": [    {      "Country name": "India",      "Flag": "IN",      "Population": 1338677000,      "undefined": "#B78A31"    }, ...]}等等?,F(xiàn)在我想像這樣組合它們:{  "countries": [    {      "Country name": "China",      "Flag": "CN",      "Population": 1395380000,      "undefined": "#688144"    }, {      "Country name": "India",      "Flag": "IN",      "Population": 1338677000,      "undefined": "#B78A31"    }, ... ]}所以我可以像這樣遍歷數(shù)據(jù):let obj1 = {}; //saved Data From Txt1;let obj2 = {}; //saved Data From Txt2...let obj = combined?for (var key in obj.countries) {var num1 = obj.countries[key].Population+popholder;if (target >= popholder && target <= num1) {  var country = obj.countries[key]['Country name'];  var testas = document.getElementById("countryname")}}我怎么能做到這一點(diǎn)?
查看完整描述

3 回答

?
動(dòng)漫人物

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

只需連接數(shù)組:


const obj1 = {

    "countries": [{

            "Country name": "China",

            "Flag": "CN",

            "Population": 1395380000,

            "undefined": "#688144"

        }

    ]

};


const obj2 = {

    "countries": [{

            "Country name": "India",

            "Flag": "IN",

            "Population": 1338677000,

            "undefined": "#B78A31"

        }

    ]

};


const result = {

    countries: [...obj1.countries, ...obj2.countries]

};


console.log(result);


查看完整回答
反對(duì) 回復(fù) 2022-12-18
?
精慕HU

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

您可以使用創(chuàng)建一個(gè)具有相同countries屬性的新對(duì)象Array#concat并將所有國(guó)家/地區(qū)數(shù)組合并為一個(gè):

const obj1 = {

  "countries": [{

    "Country name": "China",

    "Flag": "CN",

    "Population": 1395380000,

    "undefined": "#688144"

  }, /* ... */ ]

};


const obj2 = {

  "countries": [{

    "Country name": "India",

    "Flag": "IN",

    "Population": 1338677000,

    "undefined": "#B78A31"

  }, /* ... */ ]

};


const obj3 = {

  "countries": [{

    "Country name": "Sealand",

    "Flag": "",

    "Population": 27,

    "undefined": "#0000FF"

  }, /* ... */ ]

};


const combined = { 

  countries: [].concat(

    obj1.countries, 

    obj2.countries, 

    obj3.countries

  )

};


console.log(combined);

如果你有一個(gè)對(duì)象數(shù)組,你可以結(jié)合Array#map擴(kuò)展語(yǔ)法來(lái)提取countries數(shù)組并將它們組合成一個(gè):

const obj1 = { "countries": [{ "Country name": "China", "Flag": "CN", "Population": 1395380000, "undefined": "#688144" }, /* ... */ ]};

const obj2 = { "countries": [{ "Country name": "India", "Flag": "IN", "Population": 1338677000, "undefined": "#B78A31" }, /* ... */ ]};

const obj3 = { "countries": [{ "Country name": "Sealand", "Flag": "", "Population": 27, "undefined": "#0000FF" }, /* ... */ ]};


const objArr = [obj1, obj2, obj3];


const combined = { 

  combined: [].concat(

    ...objArr.map(x => x.countries)

  )

};


console.log(combined)



查看完整回答
反對(duì) 回復(fù) 2022-12-18
?
胡說(shuō)叔叔

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

你可以push所有數(shù)組合二為一。


const obj1 = {

  "countries": [

    {

      "Country name": "China",

      "Flag": "CN",

      "Population": 1395380000,

      "undefined": "#688144"

    }, ]}

const obj2 = {

  "countries": [

    {

      "Country name": "India",

      "Flag": "IN",

      "Population": 1338677000,

      "undefined": "#B78A31"

    },]};

const res = [obj1, obj2, /*...*/]

  .reduce((acc,{countries})=>(acc.countries.push(...countries),acc), {countries: []});

console.log(res);


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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