M
M
Michael Compass2022-04-17 20:14:29
JavaScript
Michael Compass, 2022-04-17 20:14:29

Array parsing and data processing in JS code. How to get past the error?

How to get past the error?
There is a task: removing from the array all lines that go above three consecutive lines at the end of which there are punctuation marks (punctuation marks are defined in the array )

There is a JS code for this (I gave it at the end of this topic). I process this array of text with code, but I get an error:

SyntaxError: Unexpected token Я in JSON at position 1


I’ll immediately explain 3 nuances:
1) A multi-line list of strings (array) in my JS interpreter is specified as a wrapping in .
For the JS language, it is an array (An array is an object)
For example, the array looks like this (see the screenshot below) the text is given here as an example, so the text can be anything:
625c4931031c8272101497.jpeg

2) Insert a multi-line array into the JS code in this form (as a string) is impossible, since the texts inside can be any:
let text = `Я видел огненные знаки
Чудес, рожденных на заре
Я вышел — пламенные маки,
Сложить на горном алтаре.
Со мною утро в дымных ризах,
Кадило в голубую твердь,
И на уступах, на карнизах`;


3) You only need to define it like this: Assign the array to the text
variable . How I try to solve this problem: In the Javascript code, first I parse using JSON.parse, converting the array into a string like this:
let text = ;




let text = ;
var qqq = JSON.parse("[" + text + "]");
let str = qqq.split(',');


And here is the entire JS code:
let text = ;
var qqq = JSON.parse("[" + text + "]");
let str = qqq.split(',');
const punct = ;
const numStrWithPunctEnd = 3;
const arr = str.split('\n');
const index = arr.findIndex(function(n, i, a) {
  return this.every(m => punct.includes(a[i + m].slice(-1)));
}, [...Array(numStrWithPunctEnd).keys()]);
const result = index !== -1 ? arr.slice(index).join('\n') : str;
=result;


When processing all the above JS code, I get an error, how to get past it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chipekwe, 2022-04-17
@XanXanXan

For the JS language, it is an array (An array is an object)
For the JS language, an array and an object are two different things.
let text =
This creates an array with one element, which is also an array. Within this internal array, one element is the variable 1AA. And here there will be errors: there is no variable 1AA to put it in a subarray; A variable name cannot start with a number.
I do parsing using JSON.parse converting the array to a string
JSON.parseon the contrary, it turns the JSON string into an object. The string textis not a JSON string and therefore this method throws an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question