javascript-objects

How to deep filter objects (not object arrays)

℡╲_俬逩灬. 提交于 2019-12-11 17:06:01
问题 I'm working with a JS data structure that looks something like this: table = { row1: { col1: 'A', col2: 'B', col3: 'C' }, row2: { col1: 'D', col2: 'A', col3: 'F' }, row3: { col1: 'E', col2: 'G', col3: 'C' } }; How would you guys filter this object using JavaScript's native filter/map/reduce functions to produce an array of row object keys that contain the property col3 = "C"? In this case, it would return ['row1', 'row3'] . This is a solution that I originally came up with based on another

Vue app, Javascript, conditionally adding an Object to an array

假装没事ソ 提交于 2019-12-11 15:58:45
问题 I have a restaurant menu which is an array of objects each of which has the following properties:- name id - the id is autogenerated quantity options - which itself is an array that has two value pairs -size & price Now, I want to allow the user to add items to the shopping cart after validating that the selected Item doesn't already exist in the cart, the problem I am facing is, each Item has an Id, but at the same time has two or more different sizes Here is my code export default { data(){

Convert object into array in IE (Javascript)

二次信任 提交于 2019-12-11 15:26:58
问题 can I convert , in Javascript, an object into an array in Internet Explorer? I read that the method Array.from(obj) is not supported from IE. It is correct? Thank you 回答1: You can verify for yourself on On Mozilla's MDN that Array.from() isn't supported by IE : On that same page, you can also find the following polyfill to add support of Array.from() to browsers that don't support it natively : // Production steps of ECMA-262, Edition 6, 22.1.2.1 if (!Array.from) { Array.from = (function () {

How to call parent method from child

前提是你 提交于 2019-12-11 13:38:27
问题 I'm getting this error when I'm trying to call pranet method: Uncaught TypeError: Cannot read property 'call' of undefined http://jsfiddle.net/5o7we3bd/ function Parent() { this.parentFunction = function(){ console.log('parentFunction'); } } Parent.prototype.constructor = Parent; function Child() { Parent.call(this); this.parentFunction = function() { Parent.prototype.parentFunction.call(this); console.log('parentFunction from child'); } } Child.prototype = Object.create(Parent.prototype);

Assign Key Value with another key value in JavaScript Object [closed]

老子叫甜甜 提交于 2019-12-11 11:54:16
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I know its possible to set a key value with a preceding key value in Javascript for example var obj = { one: "yes", two: obj.one } obj[two] is now equal to "yes" How do i go about setting the value when the keys are in a function var obj = { one: function () { return( two: "yes" three: ?? //I want

Javascript prototypal inheritance and object creation

☆樱花仙子☆ 提交于 2019-12-11 10:27:36
问题 I cant understand the differences between the normal object and inheritance creation process and the whole prototype concept. Let's say I have this code: function Person(name, age,location){ this.name = name; this.age = age; this.location = location; this.greet = function(){ return console.log(this.name + " says hi from "+this.location); } } Person.prototype = { protoGreet: function(){ console.log(this.name + " says 'that greeting was sent using a prototype'") } } var alex = new Person("Alex"

click() a button named 'submit' in Javascript

落爺英雄遲暮 提交于 2019-12-11 09:46:40
问题 I have this HTML: <input type="submit" name="submit" value="Invoeren" accesskey="s" class="buttons"/> I'd like to click() it. I can not change anything on the HTML-side. When I do getElementById("submit").click() , I get this: >>> document.getElementById("submit").click(); Cannot convert 'document.getElementById("submit")' to object Any hints? 回答1: Since you can't edit the actual HTML (to add the id attribute), and you want this to be cross-browser, you could loop over all of the input

How to create a complex/nested js object dynamically?

我们两清 提交于 2019-12-11 09:24:33
问题 I have the following data. var data = "a.b.c.d"; //Just an example but can be more deep. A nested structure as a string to create a.b.c.n Now i want to create a js object from this data like this.. { "a":{ "b":{ "c":{ and so on till the given depth. } } } } What i have tried function createHierarchy( obj, group, i){ if(i === group.length){ return obj; } else{ if(obj[group[i]] === undefined) { obj[group[i]] = new Object(); } createHierarchy(obj[group[i]], group, ++i); } } Problem This function

JS Reduce and group JSON by deeply nested object

血红的双手。 提交于 2019-12-11 08:09:33
问题 I'm pulling via REST a JSON with an array of objects with some fields and some nested objects. What I'm trying to create is a grouped summary object from the array of nested JSON objects with the following structure: var data = [ { "Id": 79, "Date": "2019-02-17T00:00:00-07:00", "StartTime": 1535385600, "EndTime": 1535416200, "Slots": [ { "blnEmptySlot": false, "strType": "B", "intStart": 3600, "intEnd": 5400, "intUnixStart": 1535389200, "intUnixEnd": 1535391000, } ], "OperationalUnit": 3,

Nested Destructuring on Object Assignments

时光怂恿深爱的人放手 提交于 2019-12-11 07:03:52
问题 With ES6 destructuring, is their any way to destructure nested objects on assignment? Here is a quick code example to show what I mean: let node = { ItemTitle: 'Title', ItemId: 5, Menu: {Item: [{ItemId: 579}] } // my attempts let { ItemId: id, ItemTitle: title, Menu['Item']: subItems } = node let { ItemId: id, ItemTitle: title, Menu.Item: subItems } = node 回答1: You can just repeat the same syntax for nested levels as with destructuring the top level: EDIT based on your comment I need the