esprima401 ArrayExpression type ArrayExpression elements Literal type Literal value 1 raw 1 Literal type Literal value 2 raw 2 Literal type Literal value 4 raw 4 这些数据不是我想要的我想要的是源代码
你可以使用Esprima库解析源代码并获取ArrayExpression节点中的元素值。以下是一个示例代码:
const esprima = require('esprima');
const sourceCode = '[1, 2, 4]';
const parsedCode = esprima.parseScript(sourceCode);
const arrayExpression = parsedCode.body[0].expression;
const elements = arrayExpression.elements.map(element => element.value);
console.log(elements); // [1, 2, 4]
这个代码首先使用Esprima库解析源代码,然后从解析结果中获取ArrayExpression节点。接下来,使用map()方法将节点中的每个元素转换为它们的值,并将这些值存储在一个数组中。最后,将数组打印到控制台上。
原文地址: https://www.cveoy.top/t/topic/92D 著作权归作者所有。请勿转载和采集!