aSarantos
aSarantos

Reputation: 1

Check if elements in array are using ASCII code

I have the following array and I was wondering if there is a way to scan it and get any ASCII codes contained inside an element.

The array looks something like this:

var elem = ["Joe", "M"+String.fromCharCode(13)+"ry", "Element_03", "Element_04"];

Attempted using a for loop to scan through the array and conditionally check each element for ASCII code but I couldn't come up with anything.

Upvotes: 0

Views: 337

Answers (2)

mrbinky3000
mrbinky3000

Reputation: 4301

If I understand your question correctly, you're trying to find non-alphanumeric characters in each string of the array. For example, CharCode 13 is a carriage return. Depending on what you consider to be "special" this might work.

var elem = ["Joe", "M"+String.fromCharCode(13)+"ry", "Element_03", "Element_04"];

var codesFound = {};

elem.join('').split('').forEach(char => {
  var code = char.charCodeAt(0);
  if ( code < 32 || code > 126 ) {
    codesFound[code] = true;
  }
});

console.log(Object.keys(codesFound));

I'm using this table as a guide. But you can get the just from my code. http://www.asciitable.com/

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138457

var hash={};
elem.forEach(function(str){
  for(var i=0;i<str.length;i++){
    hash[str.charCodeAt(i)]=true;
  }
});
console.log(Object.keys(hash));

Simply iterate over the array and chars, and add each char code into a hash table.

Upvotes: 1

Related Questions