简体   繁体   English

如何使用Spotify Web API进行变量解构

[英]How to variable deconstructing by using Spotify Web API

I want to deconstruct two variables from the chained object. 我想从链接对象中解构两个变量。

const current = track.spotify.current.metrics.new_collection_listeners;
const previous = track.spotify.previous.metrics.new_collection_listeners;

I thought it would be possible to do something like this 我认为有可能做这样的事情

const { current: new_collection_listeners } = track.spotify.current.metrics;
const { previous: new_collection_listeners } = track.spotify.previous.metrics;

But I got the error that the variable is already defined. 但是我得到的错误是该变量已经定义。

Then I thought it would be possible to do something like this: 然后我认为可以做这样的事情:

const { current: { metrics: { new_collection_listeners }}} = track.spotify
const { previous: { metrics: { new_collection_listeners }}} = track.spotify

Or 要么

const {
 previous: { metrics: { new_collection_listeners }},
 current: { metrics: { new_collection_listeners }},
} = track.spotify;

But it is saying that the new_collection_listeners is already defined and previous or current not. 但是,这是说new_collection_listeners已经定义, previouscurrent没有定义。

How do I handle this situation? 如何处理这种情况?

Both of your attempts would declare new_collection_listeners twice . 您的两次尝试都会两次声明new_collection_listeners You need to "rename" the property to a unique name. 您需要将属性“重命名”为唯一的名称。 This done using the property: variableName syntax: 这是使用property: variableName语法完成的:

const {
 previous: { metrics: { new_collection_listeners: previous }},
 //                                             ^^^^^^^^^^
 current: { metrics: { new_collection_listeners: current }},
 //.                                           ^^^^^^^^^
} = track.spotify; 

I recommend to read the MDN documentation to learn more about the desutructuring syntax. 我建议阅读MDN文档以了解有关解结构语法的更多信息。


const { current: new_collection_listeners } = track.spotify.current.metrics;
const { previous: new_collection_listeners } = track.spotify.previous.metrics;

is the same as 是相同的

const new_collection_listeners = track.spotify.current.metrics.current;
const new_collection_listeners = track.spotify.previous.metrics.previous;

const { current: { metrics: { new_collection_listeners }}} = track.spotify
const { previous: { metrics: { new_collection_listeners }}} = track.spotify

is the same as 是相同的

const new_collection_listeners = track.spotify.current.metrics.new_collection_listeners;
const new_collection_listeners = track.spotify.previous.metrics.new_collection_listeners;
const { current: new_collection_listeners } = track.spotify.current.metrics;
const { previous: new_collection_listeners } = track.spotify.previous.metrics;

would define the variable new_collection_listeners twice. 将定义变量new_collection_listeners两次。

it should be 它应该是

const { new_collection_listeners: current } = track.spotify.current.metrics;
const { new_collection_listeners: previous } = track.spotify.previous.metrics;

since it is defined like property: variableName . 因为它的定义类似于property: variableName what you did was the opposite 你所做的是相反的

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

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