js clone object with properties pointing to function [duplicate] - javascript

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 8 years ago.
In Javascript how to clone an object where one or more object properties point to a function ?
JSON.parse(JSON.stringify(object))
The above does not work because in the course of stringify it loses the reference to the function.

Try underscore library. The best solution to manipulate Array and Object.
var myCloneObject = _.clone(mySourceObject);

Related

How to merge two arrays at run time [duplicate]

This question already has answers here:
Why Doesn't Array Concatenation Work in Javascript? [closed]
(3 answers)
Closed 2 years ago.
I am getting below like values at runtime. Below is for only example. I will have same array structure at runtime. I want to merge them.
let finalSearchResult =[];
data [{"a":1000,"a":1000001,"a":10000002,"D":5000000}]
data [{"P":1000,"Q":1000001,"R":10000002,"S":5000000}]
finalSearchResult.concat(finalSearchResult,data);
but its not working. When I am printing finalSearchResult its coming as null.
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
please refer to:
Array.prototype.concat documentation
Therefore:
finalSearchResult = finalSearchResult.concat(data);
Will be the correct way to concat the arrays.

What is the difference between an object literal and a class in JavaScript? [duplicate]

This question already has answers here:
Should I be using object literals or constructor functions?
(12 answers)
Javascript Object : Literal Vs Constructor [duplicate]
(2 answers)
Object vs Class vs Function
(7 answers)
What is a difference between an object literal and a class with values in constructor in javascript?
(3 answers)
Closed 3 years ago.
I am currently finding it hard to figure out the difference between an object literal and a class.
Here's a link!
This might be helpful for you.
Content of the link:
The most significant difference I can see between creating classes and
literally notated objects is that the latter are entire objects,
whereas classes are not objects, but blueprints from which objects are
created.

Array of an already defined object in Javascript [duplicate]

This question already has an answer here:
creating arrays of objects in javascript
(1 answer)
Closed 5 years ago.
I have an object defined as
myObject = {
property1 : "",
property2 : ""
}
I need to create an Array of the above object.
I have tried using
myArray = myObject[].
But it doesn't seem to work.Is there any way that we can create an array of a predefined object in javascript.
Is there any way that we can create an array of a predefined object in javascript?
No, in JavaScript, you can't type the content of your array at the declaration of your array.
What you are looking for is TypeScript.

How can I pass an object by value to a function in nodejs/javascript? [duplicate]

This question already has answers here:
JavaScript: How to pass object by value?
(14 answers)
Closed 7 years ago.
Is there a way to pass an object by value in Javascript/NodeJS to a function? Or is the limitation built into the language?
You cannot control how JS passes variables (afaik), so it will always pass by reference (some conditions may apply). The proper way to do this is to create a copy of the object and pass that in.
This functionality is built in to jQuery (not terribly hard to replicate).
var clone = function(object) { return $.extend(true, {}, object) };
This will create a deep copy of the object which is safe to pass into methods that may have unintended side-effects.

JavaScript Calling Object Syntax [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I need to know why this isn't working. My Javascript code accesses a variable in an object. But it appears not to be working, partly because I can't figure out the syntax.
var obj = {
size:"small",
big:false,
thing:true
}
alert(obj[size]);
I'm just not sure if I got the syntax right…
This will work here.
obj.size //returns small
OR
obj["size"] //returns small
OR
var my_var = "size"
obj[my_var] //returns small
You can reference object values either by:
obj["size"]
or
obj.size
However, there is an exception. For instance, if you have following object with a number key: (Note: key is still a string even if it's defined this way):
var obj = {
1: true
};
You can retrieve it's value only by using: obj["1"]
Hence, using obj.1 will cause a syntax error.
Therefore, your code works if you change it to e.g.: alert(obj["size"]); but I prefer to use console.log(obj["size"]); for debugging. At least, if you are playing with node.js as your tags indicates.
Cheers.

Categories