javascript-objects

flattening the nested object in javascript

…衆ロ難τιáo~ 提交于 2019-12-04 09:05:50
I ran into this problem, I was able to write solution which can handle array of object (not posted here) or one level deep nested object but i couldn't solve when the given object has nested structure like below. I am curious to know how we can solve this. const source = { a: 1, b: { c: true, d: { e: 'foo' } }, f: false, g: ['red', 'green', 'blue'], h: [{ i: 2, j: 3 }] }; solution should be const solution = { 'a': 1, 'b.c': true, 'b.d.e': 'foo', 'f': false, 'g.0': 'red', 'g.1': 'green', 'g.2': 'blue', 'h.0.i': 2, 'h.0.j': 3 }; attempt for one deep nested object let returnObj = {} let

Inserting unique objects into array of objects using javascript?

限于喜欢 提交于 2019-12-04 06:28:48
问题 I am performing push operation to an array of objects. My code looks similar as shown below, var obj = {"id":1, "fname":"john", "lname":"doe"}; var userArray = [{ "id": 1, "fname": "john", "lname": "doe" }, { "id": 2, "fname": "john", "lname": "doe" }]; userArray.forEach(function (element) { if(element.id !== obj.id) { userArray.push(obj); } }); console.log(userArray); when i do push operation on userArray which already contains an object which i am pushing (from code it is var obj = {"id":1,

How can we custom sort JavaScript object keys?

亡梦爱人 提交于 2019-12-04 05:51:55
问题 I am trying to custom sort a JavaScript object but not able to get how we can do this in a right way. Say I am having an object like var test = { 'yellow': [], 'green': [], 'red': [], 'blue': [] } And I have an array with values of : var arr = ['red', 'green', 'blue', 'yellow']; So after sorting, I want to output in the order which is specified in array like var test = { 'red': [], 'green': [], 'blue': [], 'yellow': [] } But for some reason, I am not able to achieve that. What am trying to do

sessionStorage not storing original object

回眸只為那壹抹淺笑 提交于 2019-12-04 05:17:22
问题 I have an object which I'm getting by executing a function of SDK. When I try to store the object in session storage and the retrieve the object, the retrieved object looks same as original but when I perform operations on the new object I'm getting error. var xyzObject = some_function(); sessionStorage.setItem("xyzObject",xyzObject); var obj = JSON.parse(sessionStorage.getItem("xyzObject")); obj.some_other_function(); It is showing an error as obj.some_other_function is not a function.

How to implement *object* for improve my clock sample javascript program

橙三吉。 提交于 2019-12-04 04:22:36
The goal of this work is to understand and play with meaning of some object concept I've heard arround. About the bounty There is a lot of different way / approach to do this. My tries are not really clean: for adding a 2st clock, with another timezone, I have to edit 3 different places. This is not well (see at bottom of the answer). How could I do something more useful ? In the begining: post-edit: the initial question was about choosing between jquery and mootools, now choice as been made; the goal is to improve this, with the use of mootools . There is a little sample/demo i wrote to play

Is there a difference in using a constructor to create an object versus returning an object?

亡梦爱人 提交于 2019-12-04 03:38:35
问题 Is there any difference in how these functions operate? The first one is more typically of what I think about when thinking of a constructor. Example 1: using this to name and set properties. Then using new to create a new Book object. function Book(name, numPages) { this.name = name; this.numPages = numPages; } var myBook = new Book('A Good Book', '500 pages'); Example 2: returning a object by using new and just calling the function itself. function Movie(name, numMinutes) { return { name

Javascript methods that can not be called from jquery objects?

霸气de小男生 提交于 2019-12-04 03:13:08
问题 I was reading Learning jQuery 1.3 (Jonathan Chaffer and Karl Swedberg) and while sorting table, they used .get() before calling .sort() , and said we need to transform jQuery objects into array of DOM nodes. Even though jQuery objects act like arrays in many respects , they don't have any of the native array methods available, such as .sort(). Code: $("#sort").click(function() { var posts = $("#posts_div .post"); posts.sort(function(a, b) { return ($(a).text()) > ($(b).text()); }); $.each

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

好久不见. 提交于 2019-12-03 23:42:09
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: console.log(crudMode.mode); Throws: TypeError: can't redefine non-configurable property 'mode' What's wrong

Deep Flatten JavaScript Object Recursively

五迷三道 提交于 2019-12-03 22:38:40
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", "type": "Item", "items": null } ] } ] }, { "id": 2, "level": "1", "text": "Sundy", "type": "Item",

flattening the nested object in javascript

百般思念 提交于 2019-12-03 21:17:53
问题 I ran into this problem, I was able to write solution which can handle array of object (not posted here) or one level deep nested object but i couldn't solve when the given object has nested structure like below. I am curious to know how we can solve this. const source = { a: 1, b: { c: true, d: { e: 'foo' } }, f: false, g: ['red', 'green', 'blue'], h: [{ i: 2, j: 3 }] }; solution should be const solution = { 'a': 1, 'b.c': true, 'b.d.e': 'foo', 'f': false, 'g.0': 'red', 'g.1': 'green', 'g.2'