else if(input === 'remove'){
let search = prompt('what index do you want to remove');
for(let j=0;j<todos.length;j ){
console.log(`current data is:${todos[j]}`);
if(todos[j] === search){
console.log(`data to be delete is present`)
todos.splice(todos[j],1);
console.log(`data deleted is ${todos[j]}`)
}
}
if todos contains [task1, abc, def]
my output is :
current data is:task1
current data is:abc
current data is:def
data to be delete is present
data deleted is undefined
How do I remove the element that is todos[j] from the list todos[]
CodePudding user response:
You can filter()
out the unwanted elements
const todos = [ 'bla', 'ble', 'bli', 'foo', 'blo', 'blu', 'bly' ];
const search = 'foo';
const filtered = todos.filter(elem => elem !== search);
console.log(filtered);
Note that the original array will remain unchanged
CodePudding user response:
search = "def";
let todo = ["task1", "abc", "eyg7eg", "ugfuegf7uegf", "def"];
todo.forEach(function (elem, i = 0) {
if (elem === search) todo.splice(i, 1);
i ;
});
used for each for iteration the whole array to find the desired element, then if element === search is true then use splice to delete that element
CodePudding user response:
A different version "mixing" several of others mates answers:
const todos = [ 'bla', 'ble', 'bli', 'foo', 'blo', 'blu', 'bly' ];
const search = 'foo';
todos.some(
(item, index) => { if (item === search) { todo.splice(index, 1); return true;} }
);
CodePudding user response:
You can use the filter method
const todos = [ 'bla', 'ble', 'bli', 'foo', 'blo', 'blu', 'bly' ];
const search = 'foo';
Assuming the above is your todo, and the search item you want to remove from the todo
todos.filter(
(tod)=> tod !=search
)
The filter method returns an array so , you can store it in a variable. learn more about the filter method filterVideo
CodePudding user response:
You can achieve the same like this,
else if(input === 'remove'){
let search = prompt('what index do you want to remove');
const index = todos.indexOf(search);
if(index === -1){
console.log(`data not found`);
}else{
todos.splice(index,1);
console.log(`data deleted is ${search}`);
}
}
CodePudding user response:
Here are two different ways of achieving the same result:
let todos = ['mango', 'apple', 'orange', 'grape', 'strawberry']
let search = 'orange';
let search2 = 'grape';
/* FIRST */
todos = todos.filter(s => s != search);
console.log(todos);
/* SECOND */
for(let j = 0; j < todos.length; j ){
console.log(`current data is:${todos[j]}`);
if(todos[j] == search2){
console.log(`data to be delete is present`);
const removed = todos.splice(j, 1);
console.log(`data deleted is ${removed}`);
}
}
console.log(todos);
CodePudding user response:
var todos=["a1","a2","a3","a4"];
let search = prompt('what index do you want to remove');
console.warn("BEFORE")
console.warn(todos)
for(let j=0;j<todos.length;j ){
console.log(`current data is:${todos[j]}`);
if(todos[j] === search){
console.log(`data to be delete is present`)
todos.splice(j,1);
console.log(`data deleted is ${search}`)
}
}
console.warn("AFTER")
console.warn(todos)
You can run the code above, or Just change your for loop with
for(let j=0;j<todos.length;j ){
console.log(`current data is:${todos[j]}`);
if(todos[j] === search){
console.log(`data to be delete is present`)
todos.splice(j,1);
console.log(`data deleted is ${search}`)
}
}