Accessing 'this' inside the callback should reference my array

Accessing 'this' inside the callback should reference my array

Content Index :

Accessing 'this' inside the callback should reference my array
Tag : javascript , By : user182203
Date : November 28 2020, 11:01 PM

this one helps. im kinda new at javascript so i have an function which is given an array and it filters its elements from the arguments it is passed. What i need though is to make 'this' inside the callback function to refference at my originalArray so that console.log(this); would acctually print [1,2,3]. Have in mind that i acctually need to this in these lines because i can only edit all those lines not add some more. , You can use call to execute a function in a given this context:
var callback = function() {

// Using call
callback.call([1,2,3], "A", "B", "C");

// Using apply
callback.apply([1,2,3], ["A", "B", "C"]);

// Using bind:
//  creates a new function bound to [1,2,3]
callback.bind([1,2,3])("A", "B", "C");
callback.bind([1,2,3], "A")("B", "C"); // etc.
function makeMultiFilter(array) {
    // What we track
    var originalArray = array;
    var currentArray = originalArray;
    return (function arrayFilterer(pred, callback) {
        // If filter not a function return current Array
        if (typeof pred !== "function")
            return currentArray;
        // Filter out things
        currentArray = currentArray.filter(pred);

        // If callback is a function, execute callback
        if (typeof callback === "function")
            callback.call(originalArray, currentArray);
            return arrayFilterer;

var arrayFilterer = makeMultiFilter([1, 2, 3]);

// call arrayFilterer to filter out all the numbers not equal to 2
arrayFilterer(function(elem) {
    return elem != 2; // check if element is not equal to 2
}, function(currentArray) {
    console.log(this); // prints [1,2 3]
    console.log(currentArray); // prints [1,3]
}); // prints [1, 3]`

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Inside the callback in Array.prototype.map, reference the new array

Tag : javascript , By : Adrian Codrington
Date : March 29 2020, 07:55 AM
should help you out Array.prototype.map() returns a new array. I want to reference this new array inside the callback function passed as the argument to Array.prototype.map(). Can I do that? , You can't do it with .map(), but you can do it with .reduce():
someArray.reduce(function(rv, item, idx) {
  // rv is the return value, in this case your array
  rv.push({whatever: rv});
  return rv;
}, []);

JavaScript: Accessing variable inside callback

Tag : javascript , By : niel
Date : March 29 2020, 07:55 AM
With these it helps I'm trying to use the open-graph plugin in NodeJS to get a preview image for a vine. The OG result is correct, but I can't access result[i] from within the og callback - the variable is undefined. How can I access result[i] in the OG callback? , You need a closure, i keeps changing because og is async
Thing.find(function(err, result) {
    for (var i = 0; i < result.length; i++) {
        if ( result[i].attachment &&
             result[i].attachment.embed_type == 'vine') 
            (function(res) {
                og(res.attachment.embed_url, function(err, meta) {
                    res.attachment.preview_image = meta.image;

Accessing 'this' of an object inside promise callback (then)

Tag : javascript , By : UpperLuck
Date : March 29 2020, 07:55 AM
Does that help My real recommendation is not to use this or new at all (and you can use Object.create if you still want inheritance):
var SomeObject = function(config) {
    return {
        PromiseMethod1: function(){
            return somePromise(config.foo);
        PromiseMethod2: function(x){
            return someOtherPromise(config.bar, x);

var instance = SomeObject({config: true});
var SomeObject function(config) {
    this.config = config;

SomeObject.prototype.PromiseMethod1 = function(){
    return somePromise(this.config.foo);

SomeObject.prototype.PromiseMethod1 = function(x){
    return someOtherPromise(this.config.bar, x);

var instance = new SomeObject({config: true});
instance.PromiseMethod1().then(instance.PromiseMethod2.bind(instance)); //<- :(

Accessing instance variable inside of a callback function

Tag : javascript , By : August
Date : March 29 2020, 07:55 AM
may help you . Just bind your callback function to the correct context:
fs.readFile(file, (function (err, result) {

Accessing properties from inside callback ES2015

Tag : javascript , By : Yolanda N. Ceron
Date : March 29 2020, 07:55 AM
should help you out ES6 arrow functions do not override this
class ProfileManager {
  constructor($http) {
    this._http = $http;
    this.profile = {};

  getUser(profile) {
     this._http(`/api/users/${user.id}`).then((response) => {
        this.profile = response.data; 
Related Posts Related QUESTIONS :
  • How to write negative binary number?
  • Unable to set property of individual object
  • Why Named Function Expression itself cannot assign Name to another Value?
  • How to reset scroll position on single page pagination
  • How to scroll to bottom of page, when a new message is sent - Socket.io
  • How to reconstruct audio blob from a base64 encoded String?
  • How to calculate numbers within an array of objects
  • Using JavaScript to change CSS properties of SVG circles for all circles that was not clicked
  • Is there value in propTypeing repeat required properties at every level of nested components?
  • Call function if variable does not exist in a filter
  • localStorage value doesn't get updated automatically
  • React not updating state with setState correctly inside promises
  • Direct native JavaScript or jQuery method to get containing block of an element
  • Dynamic JSON object to Html Table
  • How to send Id and file to same controller?
  • How to Make a jQuery Table Cell(td) clickable to Run a Function?
  • Prevent the duplicate function onchange event jQuery
  • Reactjs Input Upload doesn't return local URL
  • An unexpected 'StartObject' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected
  • How to filter a JSON Array, with an eventListener input value at keyup
  • How to separate the different properties of filter in css?
  • Why dont work change event of bootstrap input spinner?
  • How to hide overflow text content in react
  • Is there another way to write line 6
  • Cypress - how to find by text content?
  • How to setstate for the dynamic checkboxes in a loop in react?
  • Is display:none safe?
  • How to create a form to change part of a href url of a button?
  • Put many function inside function in Javascript
  • how to exit from loop after a ajax call
  • Do Angular Reactive Form Validator.min/max check Perform Type Conversions before checking the value?
  • How to Replace Text Within Script Tags Using JQuery
  • add variable to object if not undefined using short-hand
  • How can I effectively curve my items inside a list?
  • Add new record Keyboard Shortcut on Kendo Grid Mvc
  • Time and Date running on page
  • Looping through page links puppeteer doesn't return values from the newly loaded page
  • Problem with Navigator.geoLocation.getCurrentPosition not working
  • Trying to change the id of an element with Javascript
  • Embedded Schema Inserting Blank in Meteor using Collection2
  • avoid useEffect to render at first load?
  • Why is this CSS transition only working correctly on the some clicks? How to solve it?
  • Interpolated values does not reflect the latest values in an Angular app
  • Javascript classes define attribute in constructor or outside the class
  • How to block scroll event passing through to iframe until iframe is active?
  • What is the purpose of "constructor" method in javascript?
  • Stencil JS not working with non-default imports
  • Are frameworks misleadingly named for marketing purposes?
  • React <Redirect> after transition not working
  • How to send multiple parameters in a function using EJS
  • React conditional style on custom click
  • Push a block of information to an array X times based on an external variable
  • How do I populate dropdown with names, and values (numeric ids)?
  • How to apply styling to elements created with .insertAdjacentHtml method
  • Positioning key:value of an object
  • Referencing DOM Elements with React Hooks
  • TypeError: Method "getRange" of null when using multiple scripts (Libraries) in Google scripts
  • Why can't I check if an environment variable is undefined?
  • Is there a way to find what parts are the same between two strings?
  • Every change done with magicscroll and greensock get reset when I hit the bottom of the page
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com