$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Returning an empty array instead of null - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Returning an empty array instead of null - Stack Overflow

matteradmin9PV0评论

I am wanting to return an empty array. However, it keeps returning null. How can I resolve this in my code

I am wanting the mentions array to return blank on this occasion, but it keeps ing back as null

function getTweetData(tweet) {
  let hashtag = tweet.match(/#\w+/g)
  let atSign = tweet.match(/@\w+/g)

  let tweetObj = {
    tags: hashtag,
    mentions: atSign,
    tagCount: 0,
    mentionCount: 0,
    length: tweet.length
  }

  if (tweet.match(/#/g))
    tweetObj.tagCount++

  if (tweet.match(/@/g))
    tweetObj.mentionCount++

  console.log(tweetObj)
  return tweetObj
}

Test

it('Should increase the count of tags', () => {
    expect(getTweetData('My awesome tweet about #coding')).to.eql({ tags: ['#coding'], mentions: [], tagCount: 1, mentionCount: 0, length: 30 })
  });

I am wanting to return an empty array. However, it keeps returning null. How can I resolve this in my code

I am wanting the mentions array to return blank on this occasion, but it keeps ing back as null

function getTweetData(tweet) {
  let hashtag = tweet.match(/#\w+/g)
  let atSign = tweet.match(/@\w+/g)

  let tweetObj = {
    tags: hashtag,
    mentions: atSign,
    tagCount: 0,
    mentionCount: 0,
    length: tweet.length
  }

  if (tweet.match(/#/g))
    tweetObj.tagCount++

  if (tweet.match(/@/g))
    tweetObj.mentionCount++

  console.log(tweetObj)
  return tweetObj
}

Test

it('Should increase the count of tags', () => {
    expect(getTweetData('My awesome tweet about #coding')).to.eql({ tags: ['#coding'], mentions: [], tagCount: 1, mentionCount: 0, length: 30 })
  });
Share Improve this question edited Feb 17, 2020 at 18:32 Jongware 22.5k8 gold badges55 silver badges103 bronze badges asked Jun 23, 2019 at 13:47 RowandinhoRowandinho 2037 silver badges16 bronze badges 1
  • Nothing in the code creating tweetObj attempts to create a property called mentions...? Literally, searching for mentions just finds it in the test. – T.J. Crowder Commented Jun 23, 2019 at 13:49
Add a ment  | 

2 Answers 2

Reset to default 5

You can modify:

let atSign = tweet.match(/@\w+/g)

to:

let atSign = tweet.match(/@\w+/g) || []

Adding || [] will assign an empty array to atSign in a case where your regex returns null

You can do this:

let hashtag = tweet.match(/#\w+/g) || []
let atSign = tweet.match(/@\w+/g)  || []
Post a comment

comment list (0)

  1. No comments so far