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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 JSON 文件獲取特定鍵/值對的數(shù)組

從 JSON 文件獲取特定鍵/值對的數(shù)組

慕的地6264312 2023-09-21 16:56:57
初學(xué)者 - 我想從 JSON 文件為我的世界城市類型提前項(xiàng)目創(chuàng)建一個數(shù)組。我已經(jīng)獲取了數(shù)據(jù),現(xiàn)在我想提取 2 個鍵/值對(稱為名稱/國家/地區(qū))并將它們推送到一個新的字符串?dāng)?shù)組中,該數(shù)組的語法如下:const arr = ["name, country", "name, country", "name, country"...]以下是數(shù)據(jù)示例:[]:0: {country: "Andorra", geonameid: 3040051, name: "les Escaldes", subcountry: "Escaldes-Engordany"}1: {country: "Andorra", geonameid: 3041563, name: "Andorra la Vella", subcountry: "Andorra la Vella"}2: {country: "United Arab Emirates", geonameid: 290594, name: "Umm al Qaywayn", subcountry: "Umm al Qaywayn"}3: {country: "United Arab Emirates", geonameid: 291074, name: "Ras al-Khaimah", subcountry: "Ra?s al Khaymah"}...我知道我必須使用.push()and.forEach()或 for 循環(huán),但我不知道如何去做。誰能告訴我怎么做?
查看完整描述

4 回答

?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個贊

map像這樣使用:

const?arr?=?data.map(city?=>?city.name?+?",?"?+?city.country);

arrdata將是一個與 in中的每個city對象data映射到 string 的長度相同的新數(shù)組city.name + ", " + city.country。

演示:

const data = [ {country: "Andorra", geonameid: 3040051, name: "les Escaldes", subcountry: "Escaldes-Engordany"}, {country: "Andorra", geonameid: 3041563, name: "Andorra la Vella", subcountry: "Andorra la Vella"}, {country: "United Arab Emirates", geonameid: 290594, name: "Umm al Qaywayn", subcountry: "Umm al Qaywayn"}, {country: "United Arab Emirates", geonameid: 291074, name: "Ras al-Khaimah", subcountry: "Ra?s al Khaymah"} ];


const arr = data.map(city => city.name + ", " + city.country);


console.log(arr);


查看完整回答
反對 回復(fù) 2023-09-21
?
尚方寶劍之說

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

const data = [{country: "Andorra", geonameid: 3040051, name: "les Escaldes", subcountry: "Escaldes-Engordany"},

{country: "Andorra", geonameid: 3041563, name: "Andorra la Vella", subcountry: "Andorra la Vella"},

{country: "United Arab Emirates", geonameid: 290594, name: "Umm al Qaywayn", subcountry: "Umm al Qaywayn"},

{country: "United Arab Emirates", geonameid: 291074, name: "Ras al-Khaimah", subcountry: "Ra?s al Khaymah"}]


const arr = data.map(i => `${i.name} , ${i.country}`);

console.log(arr)


查看完整回答
反對 回復(fù) 2023-09-21
?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個贊

forEach 的簡單解決方案


countries = [

  {country: "Andorra", geonameid: 3040051, name: "les Escaldes", subcountry: "Escaldes-Engordany"},

  {country: "Andorra", geonameid: 3041563, name: "Andorra la Vella", subcountry: "Andorra la Vella"},

  {country: "United Arab Emirates", geonameid: 290594, name: "Umm al Qaywayn", subcountry: "Umm al Qaywayn"},

  {country: "United Arab Emirates", geonameid: 291074, name: "Ras al-Khaimah", subcountry: "Ra?s al Khaymah"}

]


let arr = [];


let result = [];


countries.forEach((country, index) => {

    let nameStr = `${country.name}, ${country.country}`

    result.push(nameStr);

})


console.log(result)


//output

//["les Escaldes Andorra", "Andorra la Vella Andorra", "Umm al Qaywayn United Arab Emirates", "Ras al-Khaimah United Arab Emirates"]

查看完整回答
反對 回復(fù) 2023-09-21
?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個贊

如果您愿意,可以使用reduce函數(shù):


arr.reduce((acc, curr) => [...acc, (`${curr.name}, ${curr.country}`)], []);

它將以您想要的格式返回您的值。


下面的例子。


const arr = [

    {country: 'Andorra', geonameid: 3040051, name: 'les 1', subcountry: 'Escaldes-Engordany'},

    {country: 'Turkey', geonameid: 3040021, name: 'les Escaldes', subcountry: 'aaaa'},

    {country: 'IDK', geonameid: 3040021, name: 'Wow', subcountry: 'aaaa'}

];

const custom = arr.reduce((acc, curr) => [...acc, (`${curr.name}, ${curr.country}`)], []);

console.log(custom);


查看完整回答
反對 回復(fù) 2023-09-21
  • 4 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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