How to find line number by path in monaco-editor (json)?

39 Views Asked by At

json code:

{
  pages: [
    {
      text: 'test'
    }
  ]
}

getLineByPath(['pages', 0, 'text']) -> 4

1

There are 1 best solutions below

1
Adam Basha On

You can use the getPositionAt(index) function to find the line number by path.

This is how you can achieve it in js

const path = ['pages', 0, 'text']; // Your desired path
const index = model.getValue().indexOf(JSON.stringify(path));
const position = model.getPositionAt(index);

console.log(`Line number for path ${JSON.stringify(path)}: ${position.lineNumber}`);