首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要在javascript节点js中对此代码进行一些解释。

需要在javascript节点js中对此代码进行一些解释。
EN

Stack Overflow用户
提问于 2019-05-07 15:15:01
回答 1查看 42关注 0票数 0

使用下面的代码拆分包含例如5、5的字符串,但我无法理解在此之后n将包含什么。

代码语言:javascript
复制
let [,n] = parsedInput[0].split(' ');
n = +n + 2;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-07 15:25:06

代码语言:javascript
复制
// parsedInput is an array, we are getting the first element of it
// and split it considering the character space as the delimiter
// NOTE: we suppose that parsedInput[0] is a string
parsedInput[0].split(' ');
代码语言:javascript
复制
// The following syntax is called destructuring
let [,n]

//

// It's the equivalent of
const ret = parsedInput[0].split(' ');
let n = ret[1];
代码语言:javascript
复制
// The comma here means that we wants to ignore the first value, and only
// create a variable for the second one
let [,n]

// so obviously the goal here is to extract a word from a str
// example : str => 'word1 word2 word3'
// n will worth 'word2'

代码语言:javascript
复制
const parsedInput = [
  'word1 word2 word3',
];

let [, n] = parsedInput[0].split(' ');

console.log(n);

代码语言:javascript
复制
// Then this line, is converting n to a number. So we suppose what we are
// looking for in the string is a number
// and then we add 2 to it
n = +n + 2;

// It's equivalent to
const number = Number(n) + 2;

代码语言:javascript
复制
const parsedInput = [
  'word1 2 word3',
];

let [, n] = parsedInput[0].split(' ');

n = +n + 2;

console.log(n);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56025701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档