So, i'm like really new to coding in general and i'm going to tryout a coding bootcamp. So right now i'm trying to prepare myself to the "entry test" that will be focused on Javascript basics. I have this problem:
const charactersArray = [
['☆', 'D', 'a', '☆', '☆', 'r'],
['t', '☆', 'h', '☆', 'H', 'a'],
['c', 'k', '☆', 'e', '☆', 'r'],
['L', '☆', 'o', 'o', '☆', 'p'],
['S', '☆', 'k', 'y', '☆', 'w'],
['a', 'l', '☆', 'k', '☆', '☆'],
['☆', 'e', '☆', '☆', 'r', '☆']
];
let newArr = [];
for (i = 0; i < charactersArray.length; i++) {
for (j = 0; i < charactersArray[i]; j++) {
}
}
so I've got this far but i get stuck after this. I need to take off that star special letter and return a new array with only the characters. Any helping hand? I want the array to be multidimensional, yes.
Here you go
let resultAr = charactersArray.map(arr => arr.filter(item => item != "☆"));
Adding one more solution with RegEx for finer control on inclusion/exclusion
let resultAr = charactersArray.map(arr => arr.filter(item => /[^\W]/i.test(item)));
You can also simply use Array.reduce
and Array.filter
to get the desired result:
If you need one dimensional array then you can just spread the filtered arrays into a new one:
const arr = [ ['☆', 'D', 'a', '☆', '☆', 'r'], ['t', '☆', 'h', '☆', 'H', 'a'], ['c', 'k', '☆', 'e', '☆', 'r'], ['L', '☆', 'o', 'o', '☆', 'p'], ['S', '☆', 'k', 'y', '☆', 'w'], ['a', 'l', '☆', 'k', '☆', '☆'], ['☆', 'e', '☆', '☆', 'r', '☆'] ]; const result = arr.reduce((r,c) => (r.push(...c.filter(x => x !== '☆')), r), []) console.log(result)
If you need the same dimensions but filtered is the same without the spread
:
const arr = [ ['☆', 'D', 'a', '☆', '☆', 'r'], ['t', '☆', 'h', '☆', 'H', 'a'], ['c', 'k', '☆', 'e', '☆', 'r'], ['L', '☆', 'o', 'o', '☆', 'p'], ['S', '☆', 'k', 'y', '☆', 'w'], ['a', 'l', '☆', 'k', '☆', '☆'], ['☆', 'e', '☆', '☆', 'r', '☆'] ]; const result = arr.reduce((r,c) => (r.push(c.filter(x => x !== '☆')), r), []) console.log(result)
Same can be achieved in more concise manner via Array.from
:
const arr = [ ['☆', 'D', 'a', '☆', '☆', 'r'], ['t', '☆', 'h', '☆', 'H', 'a'], ['c', 'k', '☆', 'e', '☆', 'r'], ['L', '☆', 'o', 'o', '☆', 'p'], ['S', '☆', 'k', 'y', '☆', 'w'], ['a', 'l', '☆', 'k', '☆', '☆'], ['☆', 'e', '☆', '☆', 'r', '☆'] ]; const result = Array.from(arr, x => x.filter(y => y != '☆')) console.log(result)
If you need a string:
const arr = [ ['☆', 'D', 'a', '☆', '☆', 'r'], ['t', '☆', 'h', '☆', 'H', 'a'], ['c', 'k', '☆', 'e', '☆', 'r'], ['L', '☆', 'o', 'o', '☆', 'p'], ['S', '☆', 'k', 'y', '☆', 'w'], ['a', 'l', '☆', 'k', '☆', '☆'], ['☆', 'e', '☆', '☆', 'r', '☆'] ]; const result = arr.reduce((a,c) => a.concat(c.filter(x => x !== '☆').join('')), "") console.log(result)
Given OP's purpose, I don't think he/she is interested in reduce/map/filter/...(at least right now). Just a simple for loop to manipulate two-dimension array.
for (i = 0; i < charactersArray.length; i++) {
for (j = 0; j < charactersArray[i].length; j++) {
if(charactersArray[i][j] != "☆") {
newArr.push(charactersArray[i][j]);
}
}
}
You can use Array.prototype.concat()
with the spread operator to flatten the two-dimensional array, and then use Array.prototype.filter()
to filter out the stars:
const result = [].concat(...charactersArray).filter(c => c !== '☆');
Complete snippet:
const charactersArray = [ ['☆', 'D', 'a', '☆', '☆', 'r'], ['t', '☆', 'h', '☆', 'H', 'a'], ['c', 'k', '☆', 'e', '☆', 'r'], ['L', '☆', 'o', 'o', '☆', 'p'], ['S', '☆', 'k', 'y', '☆', 'w'], ['a', 'l', '☆', 'k', '☆', '☆'], ['☆', 'e', '☆', '☆', 'r', '☆'] ]; const result = [].concat(...charactersArray).filter(c => c !== '☆'); console.log(result.join('')); // DarthHackerLoopSkywalker
charactersArray.map((a)=>{
return a.filter((c)=>{
return c != '☆'});
});
->
[ [ 'D', 'a', 'r' ],
[ 't', 'h', 'H', 'a' ],
[ 'c', 'k', 'e', 'r' ],
[ 'L', 'o', 'o', 'p' ],
[ 'S', 'k', 'y', 'w' ],
[ 'a', 'l', 'k' ],
[ 'e', 'r' ] ]
It is what you want?
Here is what you might want from this requirement
I need to take off that star special letter and return a new array with only the characters.
const charactersArray = [ ['☆', 'D', 'a', '☆', '☆', 'r'], ['t', '☆', 'h', '☆', 'H', 'a'], ['c', 'k', '☆', 'e', '☆', 'r'], ['L', '☆', 'o', 'o', '☆', 'p'], ['S', '☆', 'k', 'y', '☆', 'w'], ['a', 'l', '☆', 'k', '☆', '☆'], ['☆', 'e', '☆', '☆', 'r', '☆'] ]; let newArr = []; charactersArray.forEach(function(subArrays){ let subArray = subArrays.filter(subArray => subArray !== '☆'); subArray.forEach(function(character){ newArr.push(character); }); }); console.log(newArr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.