gpt4 book ai didi

Javascript对象数组然后删除元素

转载 作者:行者123 更新时间:2023-12-03 19:51:11 25 4
gpt4 key购买 nike

我有这个对象

Object {
"87291": "valid",
"1873681927": "valid",
"nwq89j8jw0qj9": "valid",
"oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
}

我想做的是根据键值删除对象并作为数组返回。

我使用了这段代码:

    let attay = {
"87291": "valid",
"1873681927": "valid",
"nwq89j8jw0qj9": "valid",
"oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
}


let aar = Object.entries(attay)
attay = Object.keys(attay)

for(var i = 0; i < attay.length; i++) {
if(attay[i] == 'oVFYfWIUOsONE6JyMGYAbnsPMAr1'){
console.log("found "+attay[i]+" at "+i)
aar.splice(i, 1);
console.log(aar)
}else{
console.log("NOT found at "+i)
}
}

这段代码工作正常,但是删除元素后的输出是这样的

Array [
Array [
"87291",
"valid",
],
Array [
"1873681927",
"valid",
],
Array [
"nwq89j8jw0qj9",
"valid",
],
]

预期的输出应该是这样的

Array [
"87291": "valid",
"1873681927": "valid",
"nwq89j8jw0qj9": "valid",
]

最佳答案

也许我误会了,但您似乎过于复杂化了。您想从对象中删除属性吗?

let arr = {
"87291": "valid",
"1873681927": "valid",
"nwq89j8jw0qj9": "valid",
"oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid",
}

let removeKey = "oVFYfWIUOsONE6JyMGYAbnsPMAr1"
delete arr[removeKey]; // Remove the property

console.log(arr);

关于Javascript对象数组然后删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56468134/

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