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:"?


1 Answer 1


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].

  • Thank you, JP Alioto
    – vmishka
    Commented Jan 30 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.