Monwell Partee
Monwell Partee

Reputation: 718

How to pass objects through parameters of function?

I'm trying to pass objects through a function that finds words between two other words.

I'm trying to pass:


the console should log:


sentences = {
  para1: [
    cat: [
      "I love all the cats",
      "I hate cats",
      "You never had a cat before"
    ],
    dog: [
      "We can get a dog"
    ]
  ],
  para2: [
    "I",
    "You",
    "We"
  ],
  para2: [
    "cat",
    "dog"
  ]
}

function findStringBetween(str, first, last) {
  var r = new RegExp(first + "(.*)" + last)
  ab = str.match(r)
  result = ab[1].trim()
  console.log(result)
}


findStringBetween(parameter1, parameter2, parameter3);
//parameter1 should pass all of sentences.para1.cat
//parameter2 should pass all of sentences.para2
//parameter3 should pass all of sentences.para3
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Views: 56

Answers (1)

Zsolt V
Zsolt V

Reputation: 517

Your findStringBetween method accepts a single a string and you would like to use it for multiple string. You have two options:

  1. Use your method inside a loop invoking it for every member of cat.

    for(let i=0; i< cat.length(); i++)
        Use cat[i]
    
  2. You can refactor your method to accept an array instead of string. In this case you should use the loop inside your method.

Upvotes: 1

Related Questions