Reputation: 31
spt_curr_A = ["acb|123|N", "bca|234|N", "dcs_abc|134|Y", "abc|432|N", "def_bca|243|Y"]
for (var i = 0; i < spt_curr_A.length; i++){
sl = spt_curr_A[i].split("|");
console.log("Split: " + sl);
}
Once it is split, I only want the the ones with "N
" to be display as sl
. I have tried several different ways with no luck.
Upvotes: 1
Views: 105
Reputation: 6466
Arrays have methods which mean you don't always need to iterate over them manually in order to get data from them. Methods such as map
and filter
return an array, so can be chained together.
Working with arrays in this way makes code easier to read and adjust. In the example I give below, I use map
and filter
to get only the data which is useful to us. Then, forEach
allows us to iterate the remaining items and log them to the console.
If you're interested in learning more about array methods, this article is a good place to start.
Your original code could be adjusted to make a similar check:
spt_curr_A = ["acb|123|N", "bca|234|N", "dcs_abc|134|Y", "abc|432|N", "def_bca|243|Y"]
for (var i = 0; i < spt_curr_A.length; i++){
sl = spt_curr_A[i].split("|");
if (sl[2] === 'N') {
console.log("Split: " + sl);
}
}
But you may find it easier to work with code that uses map
and filter
instead of a for loop:
spt_curr_A = ["acb|123|N", "bca|234|N", "dcs_abc|134|Y", "abc|432|N", "def_bca|243|Y"]
spt_curr_A
.map(item => item.split('|'))
.filter(item => item[2] === 'N')
.forEach(item => console.log(`Split ${item}`))
Upvotes: 5
Reputation: 21766
Option 1:
Loop through the array and check if element of array contains |N
at end using str.endsWidth()
. Below is working code:
var spt_curr_A = ["acb|123|N", "bca|234|N", "dcs_abc|134|Y", "abc|432|N", "def_bca|243|Y"];
var new_array = [];
for (var i in spt_curr_A) {
if (spt_curr_A[i].endsWith("|N")) {
new_array.push(spt_curr_A[i])
}
}
console.log(new_array);
Option 2:
You can check if in sl
array element
at index 2
is N
or not like below:
var spt_curr_A = ["acb|123|N", "bca|234|N", "dcs_abc|134|Y", "abc|432|N", "def_bca|243|Y"]
for (var i = 0; i < spt_curr_A.length; i++) {
sl = spt_curr_A[i].split("|");
if (sl[2] === "N") {
console.log("Split: " + sl);
}
}
Upvotes: 1
Reputation: 2133
Do filter first and then you can do any operatin
let spt_curr_A = ["acb|123|N", "bca|234|N", "dcs_abc|134|Y", "abc|432|N", "def_bca|243|Y"]
spt_curr_A = spt_curr_A.filter((str) => {
return str.indexOf('|N') !== -1
});
spt_curr_A= spt_curr_A.splice("|")
console.log(spt_curr_A)
Upvotes: 0
Reputation: 213
What about Array.includes()?
spt_curr_A = ["acb|123|N", "bca|234|N", "dcs_abc|134|Y", "abc|432|N", "def_bca|243|Y"]
for (var i = 0; i < spt_curr_A.length; i++){
sl = spt_curr_A[i].split("|");
if(sl.includes("N"))
{
console.log("Split: " + sl);
}
}
Upvotes: 0
Reputation: 33726
An alternative is using the function Array.prototype.filter
.
Assuming every element has three sections (two pipes).
let spt_curr_A = ["acb|123|N", "bca|234|N", "dcs_abc|134|Y", "abc|432|N", "def_bca|243|Y"],
fn = (s) => s.split("|").pop() === "N",
filtered = spt_curr_A.filter(fn);
console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 0