2

How can I split this array with commas? I have multiple arrays

[ "LeadershipPress" ]
[ "BusinessLeaderLeadershipPress" ]
[ "LeaderLeadershipPoliticsPress" ]
etc.

scraper.scrapeListingPages('article.article', (item) => { 
    var categories = $(item).find('a[rel = "category tag"]').text().split();
    console.log(categories);
    categories.forEach(function(i){
       $(i).find('a[rel = "category tag"]')
       console.log(i);
    })
});

Right now my output in the console is

Array [ "BusinessLeaderLeadershipPress" ]
BusinessLeaderLeadershipPress

I want to split the categories into an array with commas without having to use separator, limits or regex because I have multiple random arrays.

Is there a way I can use a forEach or for loop to accomplish this?

The result I want is [ "Business, Leader, Leadership, Press" ]

Thanks

6
  • What is your expected output ? Commented Nov 17, 2021 at 18:03
  • There are no commas in the strings, how can you split it with commas? Commented Nov 17, 2021 at 18:03
  • You need to put the separator as the argument to split(). Otherwise it makes an array of single characters. Commented Nov 17, 2021 at 18:05
  • What is the text you're receiving? That first bit with individual categories in one-length arrays? If you're receiving that as output, apply Array.prototype.flat() to the categories array. Also see String.prototype.split() for more information on proper splitting. Commented Nov 17, 2021 at 18:10
  • do you have some examples of the strings and the wanted results? Commented Nov 17, 2021 at 18:11

2 Answers 2

2

without having to use separator, limits or regex because I have multiple random arrays.

A simple approach using for-loop with algorithm taking Consideration that string starts with Uppercase: Looping through characters of the string if char is not UPPERCASE accumelate chars to variable word, until encounter Uppercase letter, then push it in res Array.

const string = "BusinessLeaderLeadershipPress";
let i = 1;
let character = "";
let word = string[0];
const res = [];
while (i <= string.length) {
  character = string.charAt(i);
  if (character == character.toUpperCase()) {
    res.push(word);
    word = character;
  } else {
    word += character;
  }
  i++;
}
console.log(res); //['Business', 'Leader', 'Leadership', 'Press']
Sign up to request clarification or add additional context in comments.

Comments

1

You could split a string with a look ahead for an uppercase letter.

const
    string = 'BusinessLeaderLeadershipPress',
    result = string.split(/(?=[A-Z])/);

console.log(result);

1 Comment

Thank you very much. This worked! I dont know how to use regex and wasnt sure how to go about it but this was very helpful thank you!

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.