1

here I have some local function which should convert some HTML tags (keys) which contains some values into array. Every ended tag with some product have 2x '\n' which is included into array.

How can I remove breaking line from aray?

function cleanCatalog(s){
        const separateValues = /<\/?[^>]+(>|$)/g;
        let newLocal = s.replace(separateValues, " ");
        let cleanText = newLocal;
        let catalogArray = [cleanText.split("  ")];
        return catalogArray;
    }

output:

"["drill", "99", "5", "↵↵", "hammer", "10", "50", "↵↵", ...]"

3
  • Just use regular DOM methods to traverse the elements and get their text contents (+ an optional .trim()) Commented Feb 24, 2019 at 16:09
  • what is the expected output? Commented Feb 24, 2019 at 16:12
  • Hello, expected output: "["drill", "99", "5", "hammer", "10", "50", ...]" Commented Feb 24, 2019 at 16:15

4 Answers 4

2

You can use the /^\s*$/ regular expression to filter out white-spaces from the array. This can be useful if you have items like "\n\n" or "\t \n" and want to filter them too

const arr = ["drill", "99", "5", "\n \t", " ", "hammer", "10", "50", "\n\n"];
const ans = arr.filter(x => !/^\s*$/.test(x));

console.log(ans);

Sign up to request clarification or add additional context in comments.

Comments

1

Try filtering the array by excluding new lines.

const outputWithoutNewLines = output.filter(el => el !== '\n');

Comments

0

You can use filter to return: return catalogArray.filter(v=>v!='\n');

2 Comments

You mean like the solution proposed by Vladimir 10 minutes ago?
I did not try to get something special here, I got this problem: imgur.com/a/nmNjpfK I can not submit my answer (I tried to indent by 4 spaces), then I deleted everything then post again, it success but you know, I see @vladimir Bogomolov answered.
0

You could use filter function, I see you have some item with duplicate /n charactor, my solution can solve this case:

catalogArray.filter(e => e.replace(/(\r\n|\n|\r)/gm, "") !== '')

ex:

["drill", "99", "5", "\n\n", "hammer", "10", "50", "\n\n\n"].filter(e => e.replace(/(\r\n|\n|\r)/gm, "") !== '')
// =>  ["drill", "99", "5", "hammer", "10", "50"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.