javascript 我如何创建一个变量,它指向一个字符串,对象来自另一个变量?

bejyjqdl  于 2024-01-05  发布在  Java
关注(0)|答案(4)|浏览(115)

我提前道歉,如果我用词错误的问题,但我有这个问题。

  1. const restaurant = {
  2. name: 'Ichiran Ramen',
  3. address: `${Math.floor(Math.random() * 100) + 1} Johnson Ave`,
  4. city: 'Brooklyn',
  5. state: 'NY',
  6. zipcode: '11206',
  7. }

字符串
我应该创建一个新的变量,其中包含地址,城市,州和邮政编码,但我不知道如何做到这一点。
到目前为止,我试过:

  1. const fullAddress = address.city.state.zip


但我得到一个错误,说我还没有定义完整的地址变量.我真的不知道下一步该怎么办,我已经重新看了视频约5倍,现在我完全失去了.任何帮助将不胜感激.

30byixjq

30byixjq1#

你的意思是:

  1. const fullAddress = `${restaurant.address}, ${restaurant.city}, ${restaurant.state}, ${restaurant.zipcode}`

字符串
请注意,它没有在对象和构建的字符串之间创建绑定。

cdmah0mi

cdmah0mi2#

尝试

  1. let fullAddress = restaurant.address+" " +restaurant.city+ " "+restaurant.state+ " " + restaurant.zipcode

字符串

ujv3wf0j

ujv3wf0j3#

  1. let fullAddress = restaurant.name + restaurant.address + restaurant.city + restaurant.state + restaurant.zipcode
  2. // neater version
  3. //
  4. let fullAddress = restaurant.name + ", " + restaurant.address + restaurant.city + ", " + restaurant.state + ", " + restaurant.zipcode

字符串

m1m5dgzv

m1m5dgzv4#

第一种方式:

  1. let fullAddress = restaurant['address'] + ', '+ restaurant['city'] + ', '+ restaurant['state']+ ' '+restaurant['zipcode']

字符串
第二条路:

  1. let fullAddress = restaurant.address +', '+ restaurant.city +', '+ restaurant.state +' '+ restaurant.zipcode;

相关问题