1

I have some JS objects and they each have a grandchild property called 'products'.

E.g. ecommerce.add.products, ecommerce.remove.products, ecommerce.detail.products, ecommerce.checkout.products, ecommerce.purchase.products

I would like to access the products array regardless of what the specific object out of the above it is.

Tried using regex:

var ecomProducts = ecom[('detail'|'add'|'remove'|'checkout'|'purchase')]['products'];

TypeError: ecom[(((("detail" | "add") | "remove") | "checkout") | "purchase")] is undefined

var ecomProducts = ecom[/'detail'|'add'|'remove'|'checkout'|'purchase'/]['products'];

TypeError: ecom[/'detail'|'add'|'remove'|'checkout'|'purchase'/] is undefined

How can I access the nested grandchild 'products' object regardless of the parents name?

3
  • 1
    The regex that describe the above is: /ecommerce.[add|remove|detail|checkout|purchase].products/. But if you need to access - regex may not be sufficient since you have to check which pattern apply. Commented May 27, 2019 at 1:23
  • 1
    Eventually more generic way: Object.values(ecommerce).find(item => typeof item === 'object' && item !== null && !!item.products) Commented May 27, 2019 at 1:30
  • Possible duplicate of Dynamically access object property using variable Commented May 27, 2019 at 1:40

2 Answers 2

2

You can simply loop through the ecom object and check for existence of following props detail','add','remove','checkout','purchase' on ecom object,

something like this

let outerKey = ['detail','add','remove','checkout','purchase']
let foundProducts = outerKey.reduce((op,inp) => {
      if(ecom[inp] && ecom[inp].products && ecom[inp].products.length){
      op.push(ecom[inp].products)
      }  
     return op
},[])

Tried using regex:

var ecomProducts = ecom[('detail'|'add'|'remove'|'checkout'|'purchase')]['products'];

No this is not regex this is just logical OR, so you always end with ecom['checkout']['products']

Sign up to request clarification or add additional context in comments.

1 Comment

This does what I need thank you. Thanks too for the additional info on why my 'regex' was failing
1

Let's say you have an object like that::

ecommerce = { 
   add: { products: ['add products'] },
   remove: { products: ['remove'] },
   detail: { products: ['prod details'] },
   checkout: { products: ['checkout'] },
   purchase: { products: ['purchase'] }
};

    for( var x in ecommerce )"products" in ecommerce[ x ] ? 
    console.log( ecommerce[ x ].products ) : 0;

You cannot pick up apples from whichever tree you happen upon - you will need to visit the trees and the branches that contain them...

The easiest and the most portable way would be to have a function that utilizes the following expression ::

for( var x in ecommerce )"products" in ecommerce[ x ] ? 
console.log( ecommerce[ x ].products ) : 0;

and run error free.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.