Home > Blockchain >  Manipulating a json file to find the line number and length of each line in JavaScript
Manipulating a json file to find the line number and length of each line in JavaScript

Time:04-19

This is a JSON file and I would like to map through it so I can find out which line a string is on and how long that line is. for example "hello world line one chapter one" would output 1, 1,26 (chapter one, line one, 26 is the length). then hello world line three chapter two would output 2, 3, 27 ( chapter 2, line 3, 27 is the length).

how would I write a javascript/react loop/map/filter to do this?

  "text": [
    [
      "hello world line one chapter one",
      "hello world line two chapter one",
      "hello world line three chapter one"
    ],
    [
      "hello world line one chapter two",
      "hello world line two chapter two",
      "hello world line three chapter two"
    ],
    [
      "hello world line one chapter three",
      "hello world line two chapter three",
      "hello world line three chapter three"
    ]
  ]
}

CodePudding user response:

You can use the following. This uses map() and shows two different possible results, one using an array the other one using an object. Both could be valid outputs and are semantically equivalent.

const json = `{
"text": [
    [
      "hello world line one chapter one",
      "hello world line two chapter one",
      "hello world line three chapter one"
    ],
    [
      "hello world line one chapter two",
      "hello world line two chapter two",
      "hello world line three chapter two"
    ],
    [
      "hello world line one chapter three",
      "hello world line two chapter three",
      "hello world line three chapter three"
    ]
  ]
}`;

const parsed = JSON.parse(json);

const result = parsed.text.map((text, chapterIdx) =>
  text.map((line, lineIdx) => ({
    chapter: chapterIdx   1,
    line: lineIdx   1,
    length: line.length,
  }))
);

const result2 = parsed.text.map((text, chapterIdx) =>
  text.map((line, lineIdx) => [chapterIdx   1, lineIdx   1, line.length])
);

console.log("Result using JS objects:")
console.log(result);
console.log("Result using JS arrays:")
console.log(result2);

If you have the file on your disk you would of course first need to read the file using fs.readFile() in Node. Or you could import it in that case it will already be parsed and you don't need to parse it anymore.

CodePudding user response:

Wasn't sure how you wanted the output exactly, this just gives you a flat array.

let arr = { "text": [
    [
      "hello world line one chapter one",
      "hello world line two chapter one",
      "hello world line three chapter one"
    ],
    [
      "hello world line one chapter two",
      "hello world line two chapter two",
      "hello world line three chapter two"
    ],
    [
      "hello world line one chapter three",
      "hello world line two chapter three",
      "hello world line three chapter three"
    ]
  ]
}

let results = arr.text.flatMap((c,ci) => c.map((t,ti) => `${ci 1}, ${ti 1}, ${t.length}`))

console.log(results)

  • Related