Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  13] [ 6]  / answers: 1 / hits: 18739  / 8 Years ago, thu, january 5, 2017, 12:00:00

How can I remove an element using its position of an object? I for example want to remove the second one.



Object {duur: .short, taal: .nl, topic: .algemeen-management}

More From » jquery

 Answers
18

The Object.keys() will give you in the order of how it is defined. Always it is better to remove based on the key name. Because, in an object, the keys are not exactly sorted and they don't have any order.





var obj = {
duur: .short,
taal: .nl,
topic: .algemeen-management
};
console.log(obj);
var position = 2;
delete obj[Object.keys(obj)[position - 1]];
console.log(obj);





The best and right way to do is to remove by the key name:





var obj = {
duur: .short,
taal: .nl,
topic: .algemeen-management
};
console.log(obj);
var key = taal;
delete obj[key];
console.log(obj);




[#59451] Tuesday, January 3, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darleneh

Total Points: 231
Total Questions: 110
Total Answers: 94

Location: Spain
Member since Thu, Dec 23, 2021
3 Years ago
darleneh questions
;