This question already has answers here:
Looping through array and removing items, without breaking for loop
(17 answers)
Remove items from array with splice in for loop [duplicate]
(5 answers)
Closed 2 years ago.
I am trying to remove all the rows that don't have the value '2A' or '2B' but whenever I execute the code it deletes every row except the first one.
Here is my bit of code :
function supprimerColonnesInutiles() {
for(i = 1; i < data.length; i++) {
if(data[i][13] === '2019-09-30'){
data.splice(i);
}
if(data[i][2] !== '2A'){
data.splice(i);
}
}
}
This code works perfectly to delete every row with the 2019 date but fails to do what I expect it to do to delete rows that contain 2A as a value (i'd like to keep 2B values as well).
here is the csv file that serves as my array, as you can see the second column contains 2As and 2Bs but also number from 1 to 100.
This is what I get if I execute the code enter image description here
and here is my csv/array enter image description here
My question is what could be causing this issue and how can I fix it ?
Related
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
How to find if an array contains a specific string in JavaScript/jQuery?
(7 answers)
Closed 7 months ago.
while (true) {
if (memberName == subMemberGroup.indexOf("Leonardo", "Catherine", "Luther", "Bruce", "Amy") !== -1) {
console.log("\nMember's name exists in database. Please enter a new name.");
var memberName = input.question("Please enter member's name: ");
} else break;
}
I am trying to loop through to see if "memberName" that the user has input matches any of the following strings in the "subMemberGroup" array.
However, when I tried using while loop, it keeps saying that Member's name exist even thought the "memberName" that the user input does not match the strings in "subMemberGroup" array. I tried using if statement with a break but it still does not work.
The reason why I used while loop is because I want my code to re-prompt the user the enter a new name if the "memberName" matches the strings in "subMemberGroup" array.
This question already has answers here:
get the second to last item of an array?
(11 answers)
Closed 10 months ago.
I wanna to get one before last element of array in JavaScript. but in console, when I use this code "array.length -2"dose not work, and it just return me the length of array minus 2.
and when I use this code "array.at[-2]" it returns "undefined"!
const arr = [1,2,3,4,5,6]
console.log(arr[arr.length -2])
//5
This question already has answers here:
Finding items that appear only one time in a Javascript array
(5 answers)
Completely removing duplicate items from an array
(11 answers)
Closed 11 months ago.
let getNonDuplicates = [1,2,3,4,2,3,4,5]; // Here's my example array
the result I needed is to get 1 & 5 since those are the data that don't have duplicates
let resultMustBe = [1,5];
You could use filter method, to filter the array, based on the condition that index of an element from beginning is equal to index of the same element from the last. It means that the element is unique in the array.
let arr = [1,2,3,4,2,3,4,5];
let res = arr.filter(e=>arr.indexOf(e)===arr.lastIndexOf(e));
console.log(res);
This question already has answers here:
Selecting last element in JavaScript array [duplicate]
(13 answers)
Closed 6 years ago.
So say I have an array and the user can add do it by a input html tag:
var example=["a","b"];
var command=document.getElementByID("id");
so idk how long the array will be when i execute the next step,which is selecting the last item in the array and register it in an object
example.split(",")
someObject[//how do i chose the last item?]
You can select the last item in a javascript array like this
arr = example.split(",");
lastArr = arr[arr.length - 1];
This question already has answers here:
Meteor - #each iteration of an array with another HTML element inserted after each nth item
(2 answers)
Closed 6 years ago.
I'm quite new to meteor blaze. I want to know how it's possible to display something (an advert) after a particular number of iteration in meteorjs. Any ideas? Thanks in advance.
And what about doing this within the js helper? I did it in this way.
Template.foo.helpers({
stuffWithAds : function(){
col = GivenCollection.find().fetch();
toTemplate = [];
for(i=0; i<col.length; i++){
toTemplate.push(col[i]);
if (i % 2 != 0){
toTemplate.push('whatever you want to insert');
}
}
return toTemplate; }
});
This inserts whatever you want after 2 elements.