简体   繁体   English

如何使用文字 object、javascript 中的键创建变量

[英]How to make a variable using keys from literal object, javascript

In an exercise, I have to make a variable called "fullAddress" that contains everything in a given literal object except for the first key.在练习中,我必须创建一个名为“fullAddress”的变量,该变量包含给定文字 object 中除第一个键之外的所有内容。

I found a solution that sort of works, but I feel like there is a better way to do it.我找到了一种可行的解决方案,但我觉得有更好的方法来做到这一点。 Also, I can't figure out how to efficiently put space between the values in the variable.此外,我无法弄清楚如何有效地在变量中的值之间放置空间。

The exercise says the final result should look something like this:练习表明最终结果应如下所示:

39 Johnson Ave, Brooklyn, NY, 11206 39 Johnson Ave, 布鲁克林, NY, 11206

But mine looks like this:但我的看起来像这样:

39 Johnson AveBrooklynNY11206约翰逊大街 39 号布鲁克林NY11206


Literal Object provided in exercise:练习中提供的字面量 Object:

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

My solution:我的解决方案:

let fullAddress = restaurant.address + restaurant.city + restaurant.state + restaurant.zipcode; 

Given an array of strings, you can use values.join(', ') to put commas between the values cleanly.给定一个字符串数组,您可以使用values.join(', ')在值之间干净地放置逗号。 Then you could do eg [restaurant.address, restaurant.city, restaurant.state, restaurant.zipcode].join(', ') .然后你可以做例如[restaurant.address, restaurant.city, restaurant.state, restaurant.zipcode].join(', ')

If you wanted to do this more generically for any object, you could use Object.values(restaurant).slice(1).join(', ') to dynamically get all values after the first and comma delimit them.如果您想对任何 object 更通用地执行此操作,您可以使用Object.values(restaurant).slice(1).join(', ')来动态获取第一个和逗号分隔之后的所有值。

In this case though, I think it makes the most sense to just explicitly append with commas since it's an address format (maybe you don't want a space after the zip code for example), so something like restaurant.address + ', ' + restaurant.city +...不过,在这种情况下,我认为用逗号显式 append 是最有意义的,因为它是一种地址格式(例如,您可能不希望在 zip 代码之后有空格),例如restaurant.address + ', ' + restaurant.city +...

Regarding spaces (and commas), you can use a template string as done for restaurant.address .关于空格(和逗号),您可以像restaurant.address一样使用模板字符串。

 const restaurant = { name: 'Ichiran Ramen', address: `${Math.floor(Math.random() * 100) + 1} Johnson Ave`, city: 'Brooklyn', state: 'NY', zipcode: '11206', }; let fullAddress = `${restaurant.address}, ${restaurant.city}, ${restaurant.state}, ${restaurant.zipcode}`; console.log(fullAddress); // 39 Johnson Ave, Brooklyn, NY, 11206

Next, notice we're actually joining all the address-parts with the same delimiter: ', ' .接下来,请注意我们实际上使用相同的分隔符连接所有地址部分: ', ' This means we can use [].join() instead.这意味着我们可以使用[].join()代替。

 const restaurant = { name: 'Ichiran Ramen', address: `${Math.floor(Math.random() * 100) + 1} Johnson Ave`, city: 'Brooklyn', state: 'NY', zipcode: '11206', }; let addressParts = [restaurant.address, restaurant.city, restaurant.state, restaurant.zipcode]; let fullAddress = addressParts.join(', '); console.log(fullAddress); // 39 Johnson Ave, Brooklyn, NY, 11206

Lastly, (if you want to get fancy), note that restaurant.最后,(如果你想变得花哨),请注意那restaurant. is repeated.重复。 This can be avoided with [].map() .这可以通过[].map()避免。

 const restaurant = { name: 'Ichiran Ramen', address: `${Math.floor(Math.random() * 100) + 1} Johnson Ave`, city: 'Brooklyn', state: 'NY', zipcode: '11206', }; let fullAddress = ['address', 'city', 'state', 'zipcode'].map(key => restaurant[key]).join(', '); console.log(fullAddress); // 39 Johnson Ave, Brooklyn, NY, 11206

You can take out all the values from the restaurant object, remove the first element and then use join to get the desired string您可以从餐厅 object 中取出所有值,删除第一个元素然后使用 join 得到所需的字符串

 const restaurant = { name: 'Ichiran Ramen', address: `${Math.floor(Math.random() * 100) + 1} Johnson Ave`, city: 'Brooklyn', state: 'NY', zipcode: '11206', } const fullAddress = Object.values(restaurant).slice(1).join(', '); console.log(fullAddress);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM