If I can access an object from an object using list[value][index], how can I delete or unshift that object from list without using delete? (since that isn’t possible in an object list)
My object looks like this:
var list = {
'test1': [
{
example1: 'hello1'
},
{
example2: 'world1'
}
]
'test2': [
{
example1: 'hello2'
},
{
example2: 'world2'
}
]
};
After deleting an object, I want it to look like this:
var list = {
'test1': [
{
example1: 'hello1'
}
]
'test2': [
{
example1: 'hello2'
},
{
example2: 'world2'
}
]
};
When I use delete, it looks like this:
var list = {
'test1': [
{
example1: 'hello1'
},
null
]
'test2': [
{
example1: 'hello2'
},
{
example2: 'world2'
}
]
};
You can remove the object from
listby setting the value oflist[key]toundefined. This won’t remove the key, however – you’d needdeleteto do that:Is there a particular reason you don’t want to use
delete? It won’t make a difference if you’re just checkinglist['test1']for truthiness (e.g.if (list['test1']) ...), but if you want to iterate throughlistusingfor (var key in list)or something like that,deleteis a better option.EDIT: Ok, it looks like your actual question is “How can I remove a value from an array?”, since that’s what you’re doing – the fact that your array is within an object, or contains objects rather than other values, is irrelevant. To do this, use the splice() method: