JavaScript How to use an array of keys to fetch the value from a Javascript object fetch array keys in php,fetch array key jquery

If there is a Javascript object with multiple levels, as in:

myObject = {
        a: 12,
        obj11: {
                obj111: 'John',
                b:13,
                obj1111: { a:15, b: 35 } 
        },
        obj21: { 
                a:15,
                b:16 }
        }

I want to write a function to which is passed the object and an array of keys. The function should return a value based upon these keys. For example, passing [obj11,b] should return 13. Passing [obj11, obj1111,a] should return 15. Passing obj21 should return the object {a:15, b:16}

  function (myObj,myArr) {

      return keyVal;
  }

Assuming that the keys are always correct, can anyone help me solve this problem?

Answer:1

You could reduce the array with the keys and take an object as default value.

function getValue(object, path) {
    return path.reduce(function (r, k) {
        return (r || {})[k];
    }, object);
}

var object = { a: 12, obj11: { obj111: 'John', b: 13, obj1111: { a: 15, b: 35 }, obj21: { a: 15, b: 16 } } };

console.log(getValue(object, ['obj11', 'b']));
console.log(getValue(object, ['obj11', 'obj1111', 'a']));
console.log(getValue(object, ['obj11', 'obj21']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:2

I was wondering is there a way to split key/value pairs of objects and insert them into array so that I get array of objects. Here is the code: let grades = [5,5,6,7,6,7,9,10,8,6] let gradesSpread =...

I was wondering is there a way to split key/value pairs of objects and insert them into array so that I get array of objects. Here is the code: let grades = [5,5,6,7,6,7,9,10,8,6] let gradesSpread =...

  1. split object key value
  2. split object in keyshot
  3. javascript split object by key
  4. keyshot split object surfaces

I am using a layout list with Masonry (and animOnScroll on top of it). I want to add bootstrap dropdowns to my list items, but Masonry inserts directly into every dropdown <li> DOM node a style="...

I am using a layout list with Masonry (and animOnScroll on top of it). I want to add bootstrap dropdowns to my list items, but Masonry inserts directly into every dropdown <li> DOM node a style="...

I have a javascript array which get created dynamically in this format. [{prdName: "Testing2"}, {prdName: "Testing2,Testing3"}, {markets: "Testing5"}, {markets: "Testing5,Testing6"}] I want to ...

I have a javascript array which get created dynamically in this format. [{prdName: "Testing2"}, {prdName: "Testing2,Testing3"}, {markets: "Testing5"}, {markets: "Testing5,Testing6"}] I want to ...

  1. removing duplicate value in excel
  2. remove duplicate value from array in php
  3. remove duplicate value from array
  4. remove duplicate value from array javascript
  5. remove duplicate value from array in java
  6. remove duplicate value from list in python
  7. remove duplicate value from array in swift 4
  8. remove duplicate value from array in jquery
  9. how to remove duplicate values in sql
  10. remove duplicate value from list in c#
  11. remove duplicate value from list in java
  12. remove duplicate value from array swift
  13. remove duplicate value in array js
  14. remove duplicate value from multidimensional array in php
  15. remove duplicate value from arraylist in java
  16. remove duplicate value from associative array in php
  17. remove duplicate value from arraylist in android
  18. remove duplicate value online
  19. remove duplicate value in sql query
  20. remove duplicate value from array in c

Hi I want the form I'm making to submit but not before the users press ENTER key without accepting terms and conditions. I already made that but in Safari when the user press ENTER, the alert ...

Hi I want the form I'm making to submit but not before the users press ENTER key without accepting terms and conditions. I already made that but in Safari when the user press ENTER, the alert ...

  1. form submit alert javascript
  2. form submit alert in jquery
  3. form submit alert message
  4. submit form alert box
  5. html form submit alert
  6. php form submit alert
  7. form submit success alert
  8. form submit confirm alert
  9. javascript form submit alert box
  10. form submit after alert
  11. form submit sweet alert
  12. bootstrap form submit alert
  13. mvc form submit alert
  14. form submit onclick alert
  15. on submit alert form input
  16. submit form using sweet alert
  17. contact form 7 submit alert
  18. submit form alertify
  19. form submit 후 alert