javascript-objects

Dynamically access multi-level object key [duplicate]

左心房为你撑大大i 提交于 2019-12-24 11:17:32
问题 This question already has answers here : Accessing nested JavaScript objects with string key (33 answers) Javascript: How to Get Object property using Array of string? [duplicate] (7 answers) Set nested item in object/array from array of keys (3 answers) Closed 4 months ago . I have a JavaScript object, that is multiple levels deep, for example: let obj = [ { "testKeyOne": "one", "testKeyTwo": "two" }, { "testKeyThree": "three", "testKeyFour": "four", "testKeyFive": { "testKeyFiveAndAHalf":

Instancing new objects in javascript

限于喜欢 提交于 2019-12-24 08:14:35
问题 I'm probably missing something really basic on javascript knowledge, but why doesn't this work? var person = { name:"", age:0, set : function(name,age){ this.name = name; this.age = age; } } var me = new person; // ERROR: Object doesn't support this action! me.set('Victor',12); me.name //Victor But this does: var person = function(){ this.name=""; this.age=0; this.set = function(name,age){ this.name=name; this.age=age; } } var me = new person(); //WORKS! me.set('Victor',12); me.name; //Victor

How to get max property value in an object with duplicate property name?

北城以北 提交于 2019-12-24 05:06:11
问题 Suppose I have an array with the following values: Array name is "subscribers" 1: {month: "2019-07-24", subs: 2} 2: {month: "2019-07-31", subs: 3} 3: {month: "2019-08-01", subs: 2} 4: {month: "2019-08-02", subs: 3} 5: {month: "2019-08-05", subs: 3} 6: {month: "2019-08-08", subs: 4} 7: {month: "2019-08-14", subs: 5} 8: {month: "2019-08-20", subs: 7} 9: {month: "2019-08-23", subs: 7} 10: {month: "2019-08-28", subs: 8} 11: {month: "2019-08-29", subs: 11} 12: {month: "2019-09-02", subs: 2} 13:

How to check if array contains specific object [duplicate]

眉间皱痕 提交于 2019-12-24 01:42:56
问题 This question already has answers here : How to determine if Javascript array contains an object with an attribute that equals a given value? (23 answers) Closed last year . I am trying to check if array of objects contain a specific object or object with specific property. var mainArray = [ {name:"Yahya", age:"29"}, {name:"Ahmed", age:"19"}, {name:"Mohamed", age:"10"}, {name:"Ali", age:"32"}, {name:"Mona", age:"25"}, {name:"Shady", age:"62"}, {name:"Reem", age:"11"}, {name:"Marwa", age:"52"}

How to convert array of arrays to array of objects where keys are taken from the first array?

瘦欲@ 提交于 2019-12-24 01:32:31
问题 I have a variable that is returning an array of arrays, with each item in each array in double quotes. var arrayOfArrays = [ [ "Name", "Age", "Address" ], [ "A", "43", "CA" ], [ "B", "23", "VA" ], [ "C", "24", "NY" ] ] I need to convert this to the following: var arrayOfObjects = [ {"Name":"A", "Age":"43", "Address":"CA"}, {"Name":"B", "Age":"23", "Address":"VA"}, {"Name":"C", "Age":"24", "Address":"NY"} ] 回答1: Using Array.prototype.slice(), Array.prototype.map() and Array.prototype.forEach()

How to convert array of arrays to array of objects where keys are taken from the first array?

谁说胖子不能爱 提交于 2019-12-24 01:31:29
问题 I have a variable that is returning an array of arrays, with each item in each array in double quotes. var arrayOfArrays = [ [ "Name", "Age", "Address" ], [ "A", "43", "CA" ], [ "B", "23", "VA" ], [ "C", "24", "NY" ] ] I need to convert this to the following: var arrayOfObjects = [ {"Name":"A", "Age":"43", "Address":"CA"}, {"Name":"B", "Age":"23", "Address":"VA"}, {"Name":"C", "Age":"24", "Address":"NY"} ] 回答1: Using Array.prototype.slice(), Array.prototype.map() and Array.prototype.forEach()

for and forEach bypass Array property

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 00:35:14
问题 I am digging into this & Object prototype (You Don't Know JS) and I found this tricky thing that blew my mind: I created a literal Array (JS Arrays are objects) x = ['foo', 42, 'bar'] and added a property called baz like x.baz = 'baz' . Then, in Chrome dev console, just typping x will display the following result: > (3) ["foo", 42, "bar", baz: "baz"] . If I unfold the " > ", each value of the array has its own key like: 0: "foo" 1: 42 2: "bar" baz: "baz" What kind of JS-object-monster did I

Object nested property access

坚强是说给别人听的谎言 提交于 2019-12-23 21:54:58
问题 I'm trying to write a function that adds an accessor for each nested property in an object. To make this a bit clearer, given object o , and a string representing a path, I should be able to access the property at that path as a named property: var o = { child1: "foo", child2: { child1: "bar", child2: 1 child3: { child1: "baz" } } }; addAccessors(o); o["child2.child1"]; // "bar" o["child2.child2"]; // 1 o["child2.child3.child1"]; // "baz" Note that the names won't always be as uniform. Here

Move an object (element) one step up with Javascript

不想你离开。 提交于 2019-12-23 17:01:09
问题 I have several objects like this: I want to move type and value one step up so they will be next to field , and then delete data . It looks like this when departments is converted to JSON: [ {"field" : "DEPARTMAN_NO", "data" : { "type":"numeric" , "comparison":"eq" , "value":11 } }, {"field" : "DEPARTMAN_ADI", "data" : { "type":"string" , "value":"bir" } } ] I have tried: departments = grid.filters.getFilterData(); i = {}; for(var i in department) { department = i.data; delete.department.data

JavaScript: Deep check objects have same keys

╄→гoц情女王★ 提交于 2019-12-23 12:17:28
问题 Question is similar to: How can I check that two objects have the same set of property names? but only one difference I want to check: var objOne = {"a":"one","b":"two","c":{"f":"three_one"}}; var objTwo = {"a":"four","b":"five","c":{"f":"six_one"}}; have the same set of keys in all level? For example deepCheckObjKeys(objOne, objTwo) would return true where deepCheckObjKeys(objOne, objThree) return false , if: var objThree = {"a":"four","b":"five","c":{"g":"six_one"}}; Since objThree.a.c.f is