JavaScript Whitelist nested properties using this particular object format

need "whitelist" object to look like this:

{
    a: {
        b: {
            c: ''
        }
    }
}

apply to:

{
    a: {
        b: {
            c: 1
        }
        d: 2
        e: 3
    }
}

result:

{
    a: {
        b: {
            c: 1
        }
    }
}

Any suggestions? Not sure how to implement this using underscore. Was looking at _.pick but ran into trouble with the nesting.

Answer:1

Recursion with Array.prototype.reduce():

function getPaths(obj, whiteList) {  
  return Object.keys(whiteList)
    .reduce(function(whiteObj, key) {
      if (!obj.hasOwnProperty(key)) {
      } else if(typeof obj[key] === 'object') {
        whiteObj[key] = getPaths(obj[key], whiteList[key]);
      } else {
        whiteObj[key] = obj[key];
      }
    
      return whiteObj;
    }, {})
}

var whiteList = {
  a: {
    b: {
      c: ''
    }
  },
  g: ''
};

var obj = {
  a: {
    b: {
      c: 1
    },
    d: 2,
    e: 3
  }
};

var result = getPaths(obj, whiteList);

console.log(result);
Answer:2

I would write a function like this:

function filter_nested_properties_in_object(allowed_properties, original_object) {
    if (_.contains(['boolean', 'number', 'string'], typeof original_object))
        return original_object;
    var filtered_object = {};
    _.each(allowed_properties, function(allowed_value, property_name) {
        var original_value = original_object[property_name];
        if (original_value !== undefined)
            filtered_object[property_name] = filter_nested_properties_in_object(
                allowed_value, original_value);
    });
    return filtered_object;
}

Pass your "whitelist object" as allowed_properties and the object to which you want to apply it to as original_object.

Answer:3

Is there a way in either JavaScript or C# to find a user's location by their IP address without querying a third party's API?

Is there a way in either JavaScript or C# to find a user's location by their IP address without querying a third party's API?

  1. find location address
  2. find location address mobile number
  3. find address location on map
  4. find my location address
  5. find location ip address
  6. find location ip address google maps
  7. find location mac address
  8. find current location address
  9. find location email address
  10. find location using address
  11. find location by address in bing map
  12. find my location address right now
  13. find your location address
  14. find home location address
  15. find location ip address python
  16. find location using ip address
  17. find location through ip address
  18. find my current location address
  19. find ip address location on map
  20. find location via ip address

A solar monitoring system generates a page at http://68.37.209.78:8050/ with live performance data. I want to scrape and parse this data, pass the PHP variables to JavaScript variables, which I can ...

A solar monitoring system generates a page at http://68.37.209.78:8050/ with live performance data. I want to scrape and parse this data, pass the PHP variables to JavaScript variables, which I can ...

  1. loading data from s3 to redshift
  2. loading data from excel to sql server
  3. loading data from s3 to snowflake
  4. loading data from csv python
  5. loading data from excel to matlab
  6. loading data from excel to r
  7. loading data from s3 to dynamodb
  8. loading data from a package in r
  9. loading data from excel into r
  10. loading data from s3 to aurora
  11. loading data from s3 into redshift
  12. loading data from emr to redshift
  13. loading data from oracle to snowflake
  14. loading data from hdfs to hive
  15. loading data from audio device bmw
  16. loading data from excel to oracle table
  17. loading data from s3 to redshift using lambda
  18. loading data from xml to database
  19. loading data from cloud storage to bigquery
  20. loading data from hdfs to hive table

Here is a table example: <table id="tableId"> <thead> <tr> <th>line number</th> <th>value</th> </tr> </thead> <tbody> <tr&...

Here is a table example: <table id="tableId"> <thead> <tr> <th>line number</th> <th>value</th> </tr> </thead> <tbody> <tr&...

How do you pass something to a function, as if it calls the function? Say I have this function function ShowId() { alert($(this).attr('id')); } and this HTML <div id='div1'> <a class='...

How do you pass something to a function, as if it calls the function? Say I have this function function ShowId() { alert($(this).attr('id')); } and this HTML <div id='div1'> <a class='...

  1. javascript pass jquery object as parameter
  2. javascript pass jquery object to function
  3. javascript function pass jquery
  4. jquery pass javascript variable to php