javascript-objects

Error in accessor property: can't redefine non-configurable property 'status'

我们两清 提交于 2019-12-05 12:28:47
问题 I'm trying to define an object and create an accessor property for it. HTML: <input type='hidden' id='crudMode' value='Create' /> JavaScript: crudMode = { create: "Create", read: "Read", update: "Update", delete: "Delete", current: function () { return $('#crudMode').val(); } } Object.defineProperty(crudMode, 'mode', { get: function(){ return this.current(); }, set: function(value){ $('#crudMode').val(value); } }); But when I use it, it throws the mentioned error in the question title:

Deep Flatten JavaScript Object Recursively

蓝咒 提交于 2019-12-05 10:20:33
问题 Data : var data = [ { "id": 1, "level": "1", "text": "Sammy", "type": "Item", "items": [ { "id": 11, "level": "2", "text": "Table", "type": "Item", "items": [ { "id": 111, "level": "3", "text": "Dog", "type": "Item", "items": null }, { "id": 112, "level": "3", "text": "Cat", "type": "Item", "items": null } ] }, { "id": 12, "level": "2", "text": "Chair", "type": "Item", "items": [ { "id": 121, "level": "3", "text": "Dog", "type": "Item", "items": null }, { "id": 122, "level": "3", "text": "Cat

Whats the best way to remove a property from nested javascript Object?

我们两清 提交于 2019-12-05 08:09:06
I have a tree object as below, I am trying to remove the items array property if it's empty. I am not sure on the best approach to do this? I am thinking of looping through the key, check the property and then remove using delete myJSONObject[prop] ... Any thoughts / ideas are welcome? [{ text: "TreeRoot", items: [{ text: "Subgroup1", items: [] }, { text: "Subgroup2", items: [] }, { text: "Subgroup3", items: [], items: [{ text: "subgroup5", items: [{ text: "subgroup6", items: [{ text: "subgroup7", items: [{ text: "subgroup8", items: [] }] }] }] }] }] }] This should do the job ( ES5 ): function

How does object spread work if it is not an iterable?

纵然是瞬间 提交于 2019-12-05 07:36:13
I'm learning about new uses of spreading . I realize that object spread is an ES2018 proposal. It works in Node 10.5 in the following manner: const oldObj = {name:"doug", age:34}; const newObj = {...oldObj}; console.log(newObj); // { name: 'doug', age: 34 } One interesting use of spreading is to convert iterables into arrays. It works fine with Maps, for example, giving you an array of arrays of value pairs const mappie = new Map().set("name", "doug").set("age", 234).set("profession", "seeker of Cthulhu"); const arr1 = [...mappie]; console.log(arr1); // [ [ 'name', 'doug' ], [ 'age', 234 ], [

Merge objects with same key in javascript

情到浓时终转凉″ 提交于 2019-12-05 01:34:33
问题 I have been trying to figure this out for a long time, if i have an array of objects like so: var my_array = [ Object {Project: A, Hours: 2}, Object {Project: B, Hours: 3}, Object {Project: C, Hours: 5}, Object {Project: A, Hours: 6}, Object {Project: C, Hours: 9} ] I want to merge all the objects with the same key together into one object, such that their hours get added up: Expected output: my_array = [ Object {Project: A, Hours: 8} Object {Project: B, Hours: 3} Object {Project: C, Hours:

Get key value of dictionary by index in jQuery

笑着哭i 提交于 2019-12-05 01:30:46
I have a javascript dictionary object which has a pre-set keys that are defaulted to 0 . Then I need to loop through the elements of this dictionary by index and use the value of the key to set its value. Below is my code to make things easier to understand: var _map = { 'severity-normal': 0, 'severity-minimal': 0, 'severity-moderate': 0, 'severity-severe': 0, 'severity-highly-severe': 0 }; mapSeverities: function () { for (var i = 0; i < _map.length; i++) { //get the key value, ex: severity-normal, by index (which would by i) var key = //retrieved key value _map[key] = this.data(key); } } In

How to get keys (path to) the deepest nested object in a javascript nested object

…衆ロ難τιáo~ 提交于 2019-12-04 19:16:45
I have a nested javascript object like this: { "apple": { "orange": { "chilli": {}, "pineapple": { "mango": {} } }, "carrot": { "cabbage": {}, "onion": {} } } } i want to get the path (keys) of the deepest nested object. something like apple.orange.pineapple.mango any help is appriciated :) You could return an array of arrays with the longest pathes. This works for more than one path with the same length. function getDeepest(object) { return object && typeof object === 'object' ? Object.entries(object).reduce((r, [k, o]) => { var temp = getDeepest(o).reduce((r, a, i) => { if (!i || r[0].length

Advantage of using Object.create

こ雲淡風輕ζ 提交于 2019-12-04 18:34:16
问题 Similar to, but different from this question. The code below is from JavaScript: The Definitive Guide. He's basically defining an inherit method that defers to Object.create if it exists, otherwise doing plain old Javascript inheritance using constructors and swapping prototypes around. My question is, since Object.create doesn't exist on plenty of common browsers IE, what's the point of even trying to use it? It certainly clutters up the code, and one of the commenters on the previous

Javascript ES6 spread operator on undefined [duplicate]

删除回忆录丶 提交于 2019-12-04 15:26:39
问题 This question already has an answer here : Spreading undefined in array vs object (1 answer) Closed 2 years ago . While developing my react App, I needed to send a conditional prop to a component so I found somewhere a pattern to do so, although it seems really weird to me and I couldn't understand how and why it worked. If I type: console.log(...undefined) // Error console.log([...undefined]) // Error console.log({...undefined}) // Work When the spread operator is activated on undefined an

Why IIFE this keyword refers to window object..?

送分小仙女□ 提交于 2019-12-04 13:38:27
问题 When I run the below code why the IFFE this refers to window object and not to a var a = { printThis : function () { console.log('printThis', this); var inner = (function () { console.log('inner', this); })(); } }; a.printThis(); //output printThis a object inner window object <-- why..? 回答1: Consider the following example: var a = {}; var b = {}; a.hello = function() { console.log(this); }; b.hello = a.hello; In most programming languages, b.hello() would print a since they base this on