javascript-objects

ImmutableJS: merge two list of objects, without duplicating them

我们两清 提交于 2020-01-03 19:09:23
问题 Supposing I have the below: var allFoods = Immutable.List(); var frenchFood = Immutable.List([ { 'type': 'french fries', 'price': 3 }, { 'type': 'petit gateau', 'price': 40 }, { 'type': 'croissant', 'price': 20 }, ]); var fastFood = Immutable.List([ { 'type': 'cheeseburger', 'price': 5 }, { 'type': 'vegan burger', 'price': 20 }, { 'type': 'french fries', 'price': 3 } ]); I want to merge both lists, in a way that I also remove dupes (in this case, french fries ), so the expected result would

How to unshift or add to the beginning of arguments object in JavaScript

早过忘川 提交于 2020-01-03 08:29:14
问题 I've just learned the convention for popping off the first element of the arguments array (which I also learned is actually an Object ). Now I need to do the opposite. I need to use an unshift operation to add a value to the beginning of the arguments array (or Object acting like an array). Is this possible? I tried: Array.prototype.unshift.apply('hello', arguments); That had no effect on arguments whatsoever. 回答1: use .call() instead of .apply() to invoke unshift() set arguments as the this

Dynamic Javascript Tree Structure

时间秒杀一切 提交于 2020-01-03 05:13:06
问题 I would like to build the hierarchy dynamically with each node created as a layer/level in the hierarchy having its own array of nodes. THIS SHOULD FORM A TREE STRUCTURE.There should be a root node, and an undefined number of nodes and levels to make up the hierarchy size. Nothing should be fixed besides the root node. I do not need to read or search the hierarchy, I need to construct it. The array should start {"name" : "A", "children" : []} and every new node as levels would be created {

Better approach nulling variables, objects in Javascript

时光总嘲笑我的痴心妄想 提交于 2020-01-03 04:23:06
问题 I am building something for mobile and would like somehow to clear, null objects, variables to release a bit of memory. Here I have two quick examples, both are anonymous functions as I believe but what way is better or more valid approach? Sorry if I get it all wrong. To me both seem to do the same thing although I like more the first one as objects wont be created until I need it. The second version would immediately execute the code for creating variables, objects etc. but not doing the

Rendering JSON string directly as Javascript object, vs. rendering string for JSON.parse

旧街凉风 提交于 2020-01-03 03:06:07
问题 I have objects that I serialize into JSON strings on the server side for consumption by my JavaScript code. I then deliver these to the client along with the HTML content so that they are immediately available. I had been writing them into the response as strings that I then parse with JSON.parse , like this: var json = "{ \"someKey\":\"someValue\" }"; // This string written in by server-side code var parsed = JSON.parse(json); Then it occurred to me that this is a waste of time, since I

How to get all keys with values from nested objects

余生长醉 提交于 2019-12-30 23:50:32
问题 I'm looking for something kind of like Object.keys but that works for potentially nested objects. It also shouldn't include keys that have object/array values (it should only include keys with immediate string/number/boolean values). Example A Input { "check_id":12345, "check_name":"Name of HTTP check", "check_type":"HTTP" } Expected output [ "check_id", "check_name", "check_type" ] Object.keys would work for flat cases like this, but not for nested cases: Example B Input { "check_id":12345,

Why does an object literal get ignored in a statement in JavaScript?

吃可爱长大的小学妹 提交于 2019-12-30 14:51:31
问题 I was messing around the JavaScript console and noticed that >> {}[1] [1] evaluates as an array. After some more messing around I found that >> b = {}[1] undefined gives me the expected result of undefined. At first I thought the curly braces were being interpreted as a block, but attempting >> {a:1}[1] [1] still gave me the the [1] array. I would expect that if it was interpreted as a block, it would result in a syntax error. Why is this the case? More Info : I discovered from Don't

How does a jQuery instance appear as an array when called in console.log?

一世执手 提交于 2019-12-30 05:22:37
问题 When entered into a JavaScript console, a jQuery object appears as an array. However, it's still an instance of the jQuery object. var j = jQuery(); => [] console.log(j); => [] console.log('test with string concat: ' + j); => test with string concat: [object Object] j instanceof Array => false j instanceof jQuery => true How could one duplicate this with their own object? --------- EDIT --------- Thanks to ZER0 for figuring it out. Here's some example code to create an object that works just

find sum of Boolean values JavaScript object array

会有一股神秘感。 提交于 2019-12-30 04:31:08
问题 Hi i am trying to find the sum of Boolean values in the object array in JavaScript My json like be var myoBj = [{ "id": 1, "day": 1, "status": true }, { "id": 2, "day": 1, "status": false }, { "id": 3, "day": 1, "status": false }, { "id": 4, "day": 3, "status": false }]; i want the sum of all status values using reduce function in JavaScript/ typescript i want to show overall status as true only when all status are true else it should be false 回答1: var result = myObj.reduce((sum, next) => sum

Pushing objects to an array, then modifying them, makes all object properties the same [duplicate]

巧了我就是萌 提交于 2019-12-25 18:27:54
问题 This question already has answers here : Javascript pushing objects into array changes entire array (7 answers) Closed 4 years ago . When I run this code: var e = { 'id': 0, 'name': 'n' }; var data = []; for (var i = 0; i < 3; i++) { e.id = i; data.push(e); } console.log(data); I expect data to look like this: [ { 'id': 0, 'name': 'n' }, { 'id': 1, 'name': 'n' }, { 'id': 2, 'name': 'n' } ] but the actual result is: [ { 'id': 2, 'name': 'n' }, { 'id': 2, 'name': 'n' }, { 'id': 2, 'name': 'n' }