Link to array push function [duplicate] - javascript

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
Basically why I can't do this:
var a = [];
var b = a.push;
b(1);
it give's error, I know how to avoid this, but why this is not working in js?

The this value is determined by how the function is called. You can use Function#bind to ensure the this value is set.
var a = [];
var b = a.push.bind(a);
b(1);
console.log(a);

Try this:
var a = [];
var b = (param) => a.push(param);
b(1);

Related

Unable to bind javascript variable to JSON object key [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 4 months ago.
I am trying to add a value in place of json object key but it always returns variable name.
My Code:
var projectName='';
let tempArray=[];
let output={};
for(i=0;i<myJsonArray.length;i++){
name = myJsonArray[i].Project;
tempArray.push(myJsonArray[i]);
}
output= {projectName :tempArray};
console.log(JSON.stringify(output));
This returns a JSON as
{"projectName":[{"Day":"MON","Project":"ABC","Billing Rate":"xxx"},{"Day":"TUE","Project":"ABC","Billing Rate":"xyx"}]}
But I need something like this:
{"ABC":[{"Day":"MON","Project":"ABC","Billing Rate":"xxx"},{"Day":"TUE","Project":"ABC","Billing Rate":"xyx"}]}
Can someone help on what I am missing here.
Kind Regards.
You should wrap the project name into [] that would help to make a value become a key
var name = '';
let tempArray = [];
let output = {};
for (i = 0; i < myJsonArray.length; i++) {
name = myJsonArray[i].Project;
tempArray.push(myJsonArray[i]);
}
output = {
[name]: tempArray
};
console.log(JSON.stringify(output));
P/s: I don't see any projectName variable there, so I replace it by name instead.

I want to get size of Javascript object [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 2 years ago.
I created an object in JavaScript and stored some information in the object. I searched a lot for solution, but did not find any solution. I want to get the count/length of object.
Javascript Code:
productDetail = new Object();
productDetail["Code"] = "A104";
productDetail["ProductName"] = "Keyboard";
productDetail["UnitPrice"] = 10;
productDetail["Dealer"] = "AMD";
This is what you are looking for:
Object.keys(productDetail).length
You are after the count of the property keys of the object.
const productDetail = new Object();
productDetail["Code"] = "A104";
productDetail["ProductName"] = "Keyboard";
productDetail["UnitPrice"] = 10;
productDetail["Dealer"] = "AMD";
console.log(Object.keys(productDetail).length)

Javascript Function with String/Object attached to it [duplicate]

This question already has answers here:
Add method to string class
(6 answers)
Closed 4 years ago.
In javascript, I want to write a function which is called as follows:
var x = 'testString'
var y = 'anotherstring'
var z = 0
var result = x.aFunction(y, z)
function aFunction(y, z) {
...
}
This is the first time I am attempting this, my question is how can I get and use the value of x in the function aFunction, without actually referring to the declared variable.
I tried looking for this but I cannot find anything. If there is a post specifically for this that anyone knows about, please let me know.
Thanks
You need to use String.prototype.aFunction so that you can add a custom function aFunction() to the prototype of String such that it can be invoked by a string variable. Also this.toString() inside the prototype function will give you the value of the x variable (calling string)
var x = 'testString'
var y = 'anotherstring'
var z = 0
String.prototype.aFunction = function(y, z){
console.log(this.toString());
return y+z;
}
var result = x.aFunction(y, z);
console.log(result);

Why can't a variable be set to a <number>.toString(), but you can can call toString() on a return value? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 6 years ago.
function s(e) {
return e.toString();
}
var a = s(3); // works
var b = 3.toString(); // error
For example, set var a to the return value of s() that returns the first argument.toString(), but you can't set var b to 3.toString()
Javascript is expecting number(s) after the decimal, you can still do this, but you need to put your number in parenthesis:
(3).toString() = "3"

Object literals and dynamic variables [duplicate]

This question already has an answer here:
Trouble referencing variable in Collections.where method within render function
(1 answer)
Closed 9 years ago.
Can someone point me in the right direction? How do you pass a reference variable in to an object literal in JavaScript? I am using Backbone.js and specifically I am using the collections.where method. So I have something as follows:
var temp = customers.where({num: 10});
However, what if someone has a variable like var x (that changes) and they want to say something like the following:
var temp = customers.where({num: x});
JavaScript won't let you do this, I know. But how is it done or how do you get around it?
You create a closure over x like this:
var x = 10;
var filter = function() { return customers.where({num: x}); };
var temp = filter(); // uses x = 10
x = 20;
temp = filter(); // uses x = 20

Categories

Resources