gpt4 book ai didi

javascript - 在 javascript 中搜索一个字符串并使用 splice 和索引 nr 删除它

转载 作者:行者123 更新时间:2023-11-30 08:21:32 24 4
gpt4 key购买 nike

我是 js 新手,我有一个数组需要转换为对象,然后我需要搜索一个字符串,然后删除该字符串。

我可以解决的第一部分是将数组转换为对象,我还可以搜索字符串。

我无法解决的部分是检索我搜索的字符串的索引 nr,然后例如使用 splice 删除数组中的该字符串。

这是我的代码:

let todoList = ['Order dog food', 'Clean kitchen', 'Buy food', 'Do homework', 'Exercise']

function strings_to_object(array) {

// Initialize new empty array
let objects = [];


// Loop through the array
for (let i = 0; i < array.length; i++) {

// Create the object in the format you want
let obj = {"Task" : array[i]};

// Add it to the array
objects.push(obj);
}

// Return the new array
return objects;
}

//Add additional properties
let Todo = strings_to_object(todoList)
Todo[0].status = 'done'
Todo[1].status = 'done'
Todo[2].status = 'standby'
Todo[3].status = 'done'
Todo[4].status = 'standby'

console.log(Todo)

//function to find a string in the array
const findTodo = function (todos, todoTitle) {
const index = todos.findIndex(function (todo, index) {
return todo.toLowerCase() === todoTitle.toLowerCase()
})
return todoList[index]
}

//Call the function to find the string and provide the string
const todo = findTodo(todoList, 'CLean kitchen')

console.log(`You searched for: ${todo}`)

这是输出:

[ { Task: 'Order dog food', status: 'done' },
{ Task: 'Clean kitchen', status: 'done' },
{ Task: 'Buy food', status: 'standby' },
{ Task: 'Do homework', status: 'done' },
{ Task: 'Exercise', status: 'standby' } ]
You searched for: Clean kitchen

因此在上面的示例中,我想找到字符串 'Clean kitchen' 的索引 nr,然后使用 splice 将其删除。谢谢

最佳答案

我们可以从你的 findTodo 返回索引,然后使用 splice 中的索引拼接 todoList 数组

let todoList = ['Order dog food', 'Clean kitchen', 'Buy food', 'Do homework', 'Exercise']

function strings_to_object(array) {

// Initialize new empty array
let objects = [];


// Loop through the array
for (let i = 0; i < array.length; i++) {

// Create the object in the format you want
let obj = {"Task" : array[i]};

// Add it to the array
objects.push(obj);
}

// Return the new array
return objects;
}

//Add additional properties
let Todo = strings_to_object(todoList)
Todo[0].status = 'done'
Todo[1].status = 'done'
Todo[2].status = 'standby'
Todo[3].status = 'done'
Todo[4].status = 'standby'
console.log("Orginal Todo");
console.log(Todo)

//function to find a string in the array
const findTodo = function (todos, todoTitle) {
const index = todos.findIndex(function (todo, index) {
return todo.toLowerCase() === todoTitle.toLowerCase()
})
return index; // Returning Index of Element
}

//Call the function to find the string and provide the string
const todoIndex = findTodo(todoList, 'CLean kitchen') // Getting Index

//console.log(`You searched for: CLean Kitchen and found at index ${todoIndex}`)
todoList.splice(todoIndex,1); // Splicing array using index
console.log("todoList after splicing array");
console.log(todoList);
deleteFromObject("CLean kitchen"); // Delete Object from Todo
console.log("Todo after splicing object[] ");
console.log(Todo);


function deleteFromObject(todoTitle){
for(let i=0;i<Todo.length;i++){
if(Todo[i].Task.toLowerCase() == todoTitle.toLowerCase()){
Todo.splice(i,1); // Delete Object
}
}
}

这就是你想要的

关于javascript - 在 javascript 中搜索一个字符串并使用 splice 和索引 nr 删除它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52811171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com