Skip to main content

All Questions

Tagged with
7516 votes
39 answers
3.3m views

How do I remove a property from a JavaScript object?

Given an object: let myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; How do I remove the property ...
johnstok's user avatar
  • 97.7k
1028 votes
46 answers
1.5m views

Sorting object property by values

If I have a JavaScript object such as: var list = { "you": 100, "me": 75, "foo": 116, "bar": 15 }; Is there a way to sort the properties based on value? So that I end up with list = { "...
Steerpike's user avatar
  • 17.5k
977 votes
21 answers
662k views

Accessing an object property with a dynamically-computed name

I'm trying to access a property of an object using a dynamic name. Is this possible? const something = { bar: "Foobar!" }; const foo = 'bar'; something.foo; // The idea is to access something.bar, ...
RichW's user avatar
  • 10.8k
877 votes
64 answers
413k views

Test for existence of nested JavaScript object key

If I have a reference to an object: var test = {}; that will potentially (but not immediately) have nested objects, something like: {level1: {level2: {level3: "level3"}}}; What is the best way to ...
user113716's user avatar
  • 321k
454 votes
9 answers
506k views

How to create an object property from a variable value in JavaScript? [duplicate]

I want to add a new property to 'myObj', name it 'string1' and give it a value of 'string2', but when I do it it returns 'undefined: var myObj = new Object; var a = 'string1'; var b = 'string2'; ...
ecu's user avatar
  • 4,563
441 votes
12 answers
440k views

Remove array element based on object property

I have an array of objects like so: var myArray = [ {field: 'id', operator: 'eq', value: id}, {field: 'cStatus', operator: 'eq', value: cStatus}, {field: 'money', operator: 'eq', value: ...
imperium2335's user avatar
  • 23.9k
331 votes
6 answers
387k views

Is there a “not in” operator in JavaScript for checking object properties?

Is there any sort of "not in" operator in JavaScript to check if a property does not exist in an object? I couldn’t find anything about this around Google or Stack Overflow. Here’s a small snippet of ...
Aaron's user avatar
  • 10.7k
213 votes
13 answers
486k views

Getting the object's property name [duplicate]

I was wondering if there was any way in JavaScript to loop through an object like so. for(var i in myObject) { // ... } But get the name of each property like this. for(var i in myObject) { ...
Olical's user avatar
  • 40.9k
160 votes
5 answers
157k views

How to access property of anonymous type in C#?

I have this: List<object> nodes = new List<object>(); nodes.Add( new { Checked = false, depth = 1, id = "div_" + d.Id }); ... and I'm wondering if I can ...
wgpubs's user avatar
  • 8,211
135 votes
11 answers
28k views

Is it possible to get the non-enumerable inherited property names of an object?

In JavaScript we have a few ways of getting the properties of an object, depending on what we want to get. 1) Object.keys(), which returns all own, enumerable properties of an object, an ECMA5 ...
dkugappi's user avatar
  • 2,754
130 votes
7 answers
456k views

How to set a Javascript object values dynamically?

It's difficult to explain the case by words, let me give an example: var myObj = { 'name': 'Umut', 'age' : 34 }; var prop = 'name'; var value = 'Onur'; myObj[name] = value; // This does not ...
Umut KIRGÖZ's user avatar
  • 2,103
129 votes
8 answers
245k views

How do I access properties of a javascript object if I don't know the names?

Say you have a javascript object like this: var data = { foo: 'bar', baz: 'quux' }; You can access the properties by the property name: var foo = data.foo; var baz = data["baz"]; But is it ...
Adam Lassek's user avatar
  • 35.4k
129 votes
2 answers
25k views

Is it safe to delete an object property while iterating over them?

When iterating over an object's properties, is it safe to delete them while in a for-in loop? For example: for (var key in obj) { if (!obj.hasOwnProperty(key)) continue; if (shouldDelete(...
Joe Shaw's user avatar
  • 22.4k
128 votes
20 answers
98k views

Access JavaScript property case-insensitively?

Assume I have an object: var obj = { foo:"bar", fizz:"buzz" }; I need to access a property of that object dynamically like so: var objSetter = function(prop,val){ obj[prop] = val; } No ...
Matt Cashatt's user avatar
  • 23.9k
122 votes
12 answers
43k views

Calling closure assigned to object property directly

I would like to be able to call a closure that I assign to an object's property directly without reassigning the closure to a variable and then calling it. Is this possible? The code below doesn't ...
Kendall Hopkins's user avatar

15 30 50 per page
1
2 3 4 5
80