为了账号安全,请及时绑定邮箱和手机立即绑定

从 JSON 文件获取特定键/值对的数组

从 JSON 文件获取特定键/值对的数组

慕的地6264312 2023-09-21 16:56:57
初学者 - 我想从 JSON 文件为我的世界城市类型提前项目创建一个数组。我已经获取了数据,现在我想提取 2 个键/值对(称为名称/国家/地区)并将它们推送到一个新的字符串数组中,该数组的语法如下:const arr = ["name, country", "name, country", "name, country"...]以下是数据示例:[]: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 循环,但我不知道如何去做。谁能告诉我怎么做?
查看完整描述

4 回答

?
繁花如伊

TA贡献2012条经验 获得超12个赞

map像这样使用:

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

arrdata将是一个与 in中的每个city对象data映射到 string 的长度相同的新数组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);


查看完整回答
反对 回复 2023-09-21
?
尚方宝剑之说

TA贡献1788条经验 获得超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)


查看完整回答
反对 回复 2023-09-21
?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超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"]

查看完整回答
反对 回复 2023-09-21
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

如果您愿意,可以使用reduce函数:


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);


查看完整回答
反对 回复 2023-09-21
  • 4 回答
  • 0 关注
  • 114 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信