Answers.Body: <p>The structure returned is a <a href="https://stackoverflow.com/questions/5868850/creating-list-of-objects-in-javascript">list of objects</a>. Here is the schema according to the <code>chess.js</code> <a href="https://github.com/jhlywa/chess.js/blob/master/README.md#moves-piece-piece-square-square-verbose--false--" rel="nofollow noreferrer">docs</a> ...</p>
  <pre><code>chess.moves({ verbose: true }) // return verbose moves
  // -&gt; [{ color: 'w', from: 'a2', to: 'a3',
  //       flags: 'n', piece: 'p',
  //       san 'a3', 'lan', 'a2a3',
  //       before: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
  //       after: 'rnbqkbnr/pppppppp/8/8/8/P7/1PPPPPPP/RNBQKBNR b KQkq - 0 1'
  //       # a `captured` field is included when the move is a capture
  //       # a `promotion` field is included when the move is a promotion
  //     },
  //     ...
  //     ]
  </code></pre>
  <p>You would want to do something like this ...</p>
  <pre><code>var lstMvs = analysisBoard.moves({ verbose: true})
  lstMvs[26].san // for example
  </code></pre>
  <p>Your mistake is that you are indexing into a function call. <code>.moves()</code> is a function that takes a set of arguments and returns a list of structures. You must call the function and then index into the results, not the function call itself. That is <code>moves({verbose:true})[26]</code> not <code>moves[26]</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-30T20:26:46.447Z
Answers.Text: Thank you, JP Alioto
Answers.UserDisplayName: vmishka
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-30T17:05:00.647Z
Answers.LastActivityDate: 2024-01-30T17:05:00.647Z
Answers.UserDisplayName: JP Alioto
Body: <p>I am an experienced Delphi programmer but totally new to JavaScript/TypeScript and I don't understand the structure of what is returned by .moves({ verbose: true}) and the appropriate JS/TS syntax to retrieve the data from the fields of the list of possible moves. For example, the following simple code lists 50 possible moves from the position given by the FEN string.</p>
  <pre><code>import { Chess } from &quot;chess.js&quot;
  const analysisBoard = new 
  Chess(&quot;r1bq1rk1/ppppbppp/4p3/4P3/5Q2/2NB1NP1/PP1P3P/R1B1K2R w KQ - 1 11&quot;)
  console.log(analysisBoard.moves({ verbose: true}))
  </code></pre>
  <p>Move 26 in the list is both a capture and a check [san: &quot;Bxh7+&quot;]</p>
  <pre><code>26: (10) {color: &quot;w&quot;, piece: &quot;b&quot;, from: &quot;d3&quot;,...}
  color: &quot;w&quot;
  piece: &quot;b&quot;
  from: &quot;d3&quot;
  to: &quot;h7&quot;
  san: &quot;Bxh7+&quot;
  flags: &quot;c&quot;
  lan: &quot;d3h7&quot;
  before: &quot;r1bq1rk1/ppppbppp/4p3/4P3/5Q2/2NB1NP1/PP1P3P/R1B1K2R w KQ - 1 11&quot;
  after: &quot;r1bq1rk1/ppppbppB/4p3/4P3/5Q2/2N2NP1/PP1P3P/R1B1K2R b KQ - 0 11&quot;
  captured: &quot;p&quot;
  </code></pre>
  <p>I thought that the structure might be just a list of strings. However, syntax like let <code>MoveStr=analysisBoard.moves[26]</code>, isn't getting me anywhere. When I try <code>console.log(MoveStr)</code>, it is undefined.</p>
  <p>Would someone who is familiar with the structure and the appropriate JS/TS syntax please be so kind as to tell me how I would loop through the available moves, finding any that are checks (i.e., contain the character &quot;+&quot;) and/or are captures (either they contain the character &quot;x&quot;, or flags: contains &quot;c&quot; or the captured field is present) and then be able to access the data from some of the fields of those moves, for example, &quot;piece:&quot;, &quot;from:&quot;, &quot;san:&quot; and &quot;after:&quot;?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-01-30T17:03:45.153Z
Comments.Text: This looks like JSON. I am rusty at using it and don't have time right now to refresh my knowledge and figure out a solution, but you might look at something like this: https://www.digitalocean.com/community/tutorials/how-to-work-with-json-in-javascript Or ask on https://stackoverflow.com
Comments.UserDisplayName: GreenMatt
Tags: <software>
Title: Chess.js -- How to access the various fields in .moves({ verbose: true})
ThreadText: # Chess.js -- How to access the various fields in .moves({ verbose: true})
  
  (Asked by: username_0 on 2024-01-30)
  
  I am an experienced Delphi programmer but totally new to JavaScript/TypeScript and I don't understand the structure of what is returned by .moves({ verbose: true}) and the appropriate JS/TS syntax to retrieve the data from the fields of the list of possible moves. For example, the following simple code lists 50 possible moves from the position given by the FEN string.
  import { Chess } from "chess.js"
  const analysisBoard = new 
  Chess("r1bq1rk1/ppppbppp/4p3/4P3/5Q2/2NB1NP1/PP1P3P/R1B1K2R w KQ - 1 11")
  console.log(analysisBoard.moves({ verbose: true}))
  
  Move 26 in the list is both a capture and a check [san: "Bxh7+"]
  26: (10) {color: "w", piece: "b", from: "d3",...}
  color: "w"
  piece: "b"
  from: "d3"
  to: "h7"
  san: "Bxh7+"
  flags: "c"
  lan: "d3h7"
  before: "r1bq1rk1/ppppbppp/4p3/4P3/5Q2/2NB1NP1/PP1P3P/R1B1K2R w KQ - 1 11"
  after: "r1bq1rk1/ppppbppB/4p3/4P3/5Q2/2N2NP1/PP1P3P/R1B1K2R b KQ - 0 11"
  captured: "p"
  
  I thought that the structure might be just a list of strings. However, syntax like let MoveStr=analysisBoard.moves[26], isn't getting me anywhere. When I try console.log(MoveStr), it is undefined.
  Would someone who is familiar with the structure and the appropriate JS/TS syntax please be so kind as to tell me how I would loop through the available moves, finding any that are checks (i.e., contain the character "+") and/or are captures (either they contain the character "x", or flags: contains "c" or the captured field is present) and then be able to access the data from some of the fields of those moves, for example, "piece:", "from:", "san:" and "after:"?
  
  **Tags:** software
  
  > **Comments:**
  > **username_1**: This looks like JSON. I am rusty at using it and don't have time right now to refresh my knowledge and figure out a solution, but you might look at something like this: https://www.digitalocean.com/community/tutorials/how-to-work-with-json-in-javascript Or ask on https://stackoverflow.com  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2024-01-30. Score: 0 | Accepted answer
  
  The structure returned is a list of objects. Here is the schema according to the chess.js docs ...
  chess.moves({ verbose: true }) // return verbose moves
  // -> [{ color: 'w', from: 'a2', to: 'a3',
  //       flags: 'n', piece: 'p',
  //       san 'a3', 'lan', 'a2a3',
  //       before: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
  //       after: 'rnbqkbnr/pppppppp/8/8/8/P7/1PPPPPPP/RNBQKBNR b KQkq - 0 1'
  //       # a `captured` field is included when the move is a capture
  //       # a `promotion` field is included when the move is a promotion
  //     },
  //     ...
  //     ]
  
  You would want to do something like this ...
  var lstMvs = analysisBoard.moves({ verbose: true})
  lstMvs[26].san // for example
  
  Your mistake is that you are indexing into a function call. .moves() is a function that takes a set of arguments and returns a list of structures. You must call the function and then index into the results, not the function call itself. That is moves({verbose:true})[26] not moves[26].
  
  > **Comments:**
  > **username_0**: Thank you, username_2  
  
  ---
  
  