Comments (4)
1.item.node是没有attr方法的,item有attr方法
2.item.match[1][0]包含两个属性,value和node,value只读,node是具体的节点,可以进行修改,节点结构可以断点查看。因此操作item.match[1][0].node就ok了:
https://play.gogocode.io/#code/N4IglgdgDgrgLgYQPYBMCmIBcIDGSIDOcABOgTgDJhECMxAvMQNrAA6Exxk6AHpsTQA07ThACGAWzT8A5OKk0ZwjqTTlZgM21A+K5L2AX0HE2K7mj7EATMtGTpxObYu6VZHLMCapoAg9GfsPHOpuYAzNbE8nYOUkHOnK6ygBXK3voAugDcIIIgAO5IAE4A1sjoWCAAZjAQOHBg+MRwuWKEpXkSABSlYAA2aACSEM2GYlBghkhQ1fgEAJRGIsR4hCQAJAzEQ2AAdADmSDt46HMLRMQESDC5OGirHd19zRun55dzAPQvxIAU6oCb8YAziYAr8YA9tUABvJaQAIRoAQt0Ax3KAQA9DpMSEgAEYAK1W-ns2hksiIMHQEDgMXsXix9jgaDEOAAFmhcoSZIkSTI8Eh8jSfBA9HNcmg4OcOEtWo8LmgpnMNh0IChWgADYBhWz8JYAfSWQlU6mIyqWFj00tFKg25KprTAZIkDAAfLMVJw3lwzRsIKg0Kw4K7XXbAKDKgGoVPCdPKAPR1AOQG4MAn9qAQxi5rb3qa0BINhIxHAqe64Ha-XlwYAeBXYUfm8PlUgAamJOjAroxY-HE8nKUwaMkmAAGZIbABupfLqTzRxIrhLZYr9rjCaTVKYFkbLfbnbQ3Zt+cWa2OlYdTvQ884AQdNfHDebrfXaBng9WSORTH7s+Scz0+s4G1ySCQcFa9+I2zQEBpSbQb-07AZOA0DwBQjRbCUcAAJ5QGoOC5GA4xAZSYgEAACtycDVDSWD1OWmR4BIUBiNyADy8CwHAuG5OWehAA
from gogocode.
非常感谢!
from gogocode.
const $ = require('gogocode');
const obj = {
'学生': 'student',
'教师': 'teacher',
'厨师': 'cooker'
}
const res = $(`
const descList1 = [{
index: 1,
name: 'name1',
desc: '学生',
}, {
index: 2,
name: 'name2',
desc: '教师'
}, {
index: 3,
name: 'name3',
desc: '厨师'
}];
`)
.find(`{ name: $_$1, desc: $_$2}`)
.each(item => {
// item.node // 包含color属性的对象
// item.match // color的值
const nameValue = item.match[1][0].value;
const descValue = item.match[2][0].value;
const ast = item.node;
console.log('name', nameValue,'->',obj[descValue]);
// TODO: 这块代码,显示错误拿不到attr属性
ast.attr("name", obj[descValue])
})
.root()
.generate()
console.log('res', res);
ast.attr("name", obj[descValue])
^
TypeError: ast.attr is not a function
我想要直接修改这个ast的name属性,该怎么弄?
from gogocode.
AST对象才有attr方法,例如demo里的item.attr('xxx'),它的node是没有attr的
另外这种对象操作直接用js操作更方便些
from gogocode.
Related Issues (20)
- 查找import 重写from 路径报错 HOT 1
- Bug Report From Playground: Your Problem
- ELloading 不兼容导致的报错
- find template 转换报错
- `gogo-code-plugin-prettier` misplacement for innerHTML text with no surrounding spaces
- 报错 SyntaxError: Unexpected closing tag "b",检查代码没有问题 HOT 1
- Bug Report From Playground: ast.find('<template></template>')报错
- 插件编写
- @element-plus/icons is deprecated, so you need to convert it with @element-plus/icons-vue HOT 1
- 可以支持 options API 写法 转 composition API 写法吗 HOT 3
- 即将4202了还有人从B站过来吗?
- 模版字符串如何用函数包裹? HOT 1
- 升级vue是排查多个规则不生效问题 HOT 1
- BUG: ast cannot be parsed cause replace `export class` is work when source is export function HOT 1
- the gogocode.io website is crashed HOT 1
- https://gogocode.io/ 官网挂了 有其他地方可以看文档吗 HOT 4
- style模块转换时,如果存在注释内容,则转化后的代码有问题
- 卡着不动 HOT 1
- The use of import.meta.env causes the AST parsing to fail. HOT 1
- 转换computed cnanot be parsed!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gogocode.