title: iOS Swift: When I pass data like array or dictionary to Javascript means it returns Undefined value?
body: <p>In my project I have pass String from swift to JavaScript and displaying in alert it works perfectly. But if I have pass array or dictionary means returns undefined values like </p>
  
  <blockquote>
    <p>[object Object],[object Object],[object Object],[object
    Object],[object Object],[object Object]</p>
  </blockquote>
  
  <p>but I don't know how to fix the issue.</p>
  
  <p>Here I have attached the screen shot for webview response.</p>
  
  <p><a href="https://i.stack.imgur.com/bO8Rb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bO8Rb.png" alt="I displayed the returned value in alert"></a></p>
  
  <p>Here I have share my code what I am tried,</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      webView.delegate = self
      loadHTMLFromBundle()
  }
  func loadHTMLFromBundle(){
      let url = Bundle.main.url(forResource: "index", withExtension: "html")
      let urlRequest:URLRequest = URLRequest(url:url!)
      self.webView.loadRequest(urlRequest)
  }
  
  func getComponents() {
      guard let path = Bundle.main.path(forResource: "components", ofType: "txt") else { return }
      let url = URL(fileURLWithPath: path)
  
      let jsonData = try! Data(contentsOf: url)
      tempComponentArray = try! JSONSerialization.jsonObject(with: jsonData, options: []) as! NSArray
      print("jsonDict:\n \(tempComponentArray)")
  
      do {
          let jsonData = try JSONSerialization.data(withJSONObject: tempComponentArray, options: [])
  
          //Convert back to string. Usually only do this for debugging
          if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
              print(JSONString)
              let urlString:NSString = NSString(format: "myNewFunction(%@)", JSONString)
              print("urlString: ",urlString)
              self.templateFormStr = JSONString
              print(self.templateFormStr)
              let encodedStr = self.webView.stringByEvaluatingJavaScript(from: "myNewFunction('\(self.templateFormStr)')")   **-----------&gt; Here I passed or evaluate the data to javascript**
              print("encodedStr: ",encodedStr!)
          }
  
      } catch {
          print(error.localizedDescription)
      }
  }
  
  @IBAction func loadJSBtnTapped(_ sender: Any) {
      getComponents()
  }
  </code></pre>
  
  <p>Here I share the Array Values,</p>
  
  <pre><code>[
    {
      "placeholder": "Enter the comments",
      "input": true,
      "showWordCount": false,
      "label": "North",
      "showCharCount": false,
      "type": "textfield",
      "allowMultipleMasks": false,
      "key": "textField"
    },
    {
      "placeholder": "Enter the comments",
      "input": true,
      "showWordCount": false,
      "label": "South",
      "showCharCount": false,
      "type": "textfield",
      "allowMultipleMasks": false,
      "key": "south"
    },
    {
      "placeholder": "Enter the comments",
      "input": true,
      "showWordCount": false,
      "label": "East",
      "showCharCount": false,
      "type": "textfield",
      "allowMultipleMasks": false,
      "key": "east"
    },
    {
      "placeholder": "Enter the comments",
      "input": true,
      "showWordCount": false,
      "label": "West",
      "showCharCount": false,
      "type": "textfield",
      "allowMultipleMasks": false,
      "key": "west"
    },
    {
      "placeholder": "Enter the comments",
      "input": true,
      "showWordCount": false,
      "label": "Easements / Encroachments",
      "showCharCount": false,
      "type": "textfield",
      "allowMultipleMasks": false,
      "key": "easementsEncroachments"
    },
    {
      "placeholder": "Enter the comments",
      "input": true,
      "showWordCount": false,
      "label": "Soil ans Sub-Soil Conditions",
      "showCharCount": false,
      "type": "textfield",
      "allowMultipleMasks": false,
      "key": "soilAnsSubSoilConditions"
    },
    {
      "placeholder": "Enter the comments",
      "input": true,
      "showWordCount": false,
      "label": "Environmental Conditions",
      "showCharCount": false,
      "type": "textfield",
      "allowMultipleMasks": false,
      "key": "environmentalConditions"
    }
  ]
  </code></pre>
  
  <p>Here I have share my HTML code what I am tried,</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;p&gt;Click the button to display an alert box.&lt;/p&gt;
      &lt;script&gt;
      function myNewFunction(param) {
          var arrayData = JSON.parse(param);
          alert(arrayData)
      }
      &lt;/script&gt;
  &lt;body&gt;
      &lt;button onclick="myFunction()"&gt;Try it&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When Display unparsed Json means alert shows empty, Here I have attached the screenshot.</p>
  
  <p><a href="https://i.stack.imgur.com/raSWR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/raSWR.png" alt="enter image description here"></a></p>