0

This is a follow-up question to this question:

getPathValue() function for deep objects

I NEED TO KEEP ALL OF THE FUNCTIONALITY OF THE ORIGINAL SOLUTION:

function getPathValue(object, path) {
    return path
       .match(/[^[\].]+/g)
       .reduce(function (o, k) {
           return (typeof o === 'string' ? JSON.parse(o) : (o || {}))[k];
       }, object);
}

I would like to modify this function so that it can handle a particular type of array and return MULTIPLE values, separated by commas.

Here is the sample object:

{"MSFT":{"company":{"symbol":"MSFT","companyName":"Microsoft Corporation","exchange":"Nasdaq Global Select","industry":"Application Software","website":"http://www.microsoft.com","description":"Microsoft Corp is a technology company. It develop, license, and support a wide range of software products and services. Its business is organized into three segments: Productivity and Business Processes, Intelligent Cloud, and More Personal Computing.","CEO":"Satya Nadella","issueType":"cs","sector":"Technology","tags":["Technology","Software - Infrastructure","Application Software"]}},"T":{"company":{"symbol":"T","companyName":"AT&T Inc.","exchange":"New York Stock Exchange","industry":"Communication Services","website":"http://www.att.com","description":"AT&T Inc is engaged in provision of communications and digital entertainment services in the United States and the world. It provides fixed-line services, including voice, data, and television services to consumers and small businesses.","CEO":"Randall L. Stephenson","issueType":"cs","sector":"Communication Services","tags":["Communication Services","Telecom Services"]}},"GE":{"company":{"symbol":"GE","companyName":"General Electric Company","exchange":"New York Stock Exchange","industry":"Industrial Products","website":"http://www.ge.com","description":"General Electric Co is a digital industrial company. It operates in various segments, including power and water, oil and gas, energy management, aviation, healthcare, transportation, appliances and lighting, and more.","CEO":"H. Lawrence Culp","issueType":"cs","sector":"Industrials","tags":["Industrials","Diversified Industrials","Industrial Products"]}},"FB":{"company":{"symbol":"FB","companyName":"Facebook Inc.","exchange":"Nasdaq Global Select","industry":"Online Media","website":"http://www.facebook.com","description":"Facebook Inc is the world's largest online social network. Its products are Facebook, Instagram, Messenger, WhatsApp, and Oculus. Its products enable people to connect and share through mobile devices and personal computers.","CEO":"Mark Zuckerberg","issueType":"cs","sector":"Technology","tags":["Technology","Internet Content & Information","Online Media"]}},"AAPL":{"company":{"symbol":"AAPL","companyName":"Apple Inc.","exchange":"Nasdaq Global Select","industry":"Computer Hardware","website":"http://www.apple.com","description":"Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.","CEO":"Timothy D. Cook","issueType":"cs","sector":"Technology","tags":["Technology","Consumer Electronics","Computer Hardware"]}},"NKE":{"company":{"symbol":"NKE","companyName":"Nike Inc.","exchange":"New York Stock Exchange","industry":"Manufacturing - Apparel & Furniture","website":"http://www.nike.com","description":"Nike Inc designs, develops and markets footwear, apparel, equipment, and accessory products. It is a seller of athletic footwear and athletic apparel. It sells its products through NIKE-owned in-line and factory retail stores and internet websites.","CEO":"Mark G. Parker","issueType":"cs","sector":"Consumer Cyclical","tags":["Consumer Cyclical","Footwear & Accessories","Manufacturing - Apparel & Furniture"]}},"KO":{"company":{"symbol":"KO","companyName":"Coca-Cola Company (The)","exchange":"New York Stock Exchange","industry":"Beverages - Non-Alcoholic","website":"http://www.coca-colacompany.com","description":"Coca-Cola Co is nonalcoholic beverage company which manufactures a variety of carbonated and noncarbonated brands, including Coca-Cola, Diet Coke, Fanta, Sprite, Minute Maid, Powerade, and Dasani.","CEO":"James Robert Quincey","issueType":"cs","sector":"Consumer Defensive","tags":["Consumer Defensive","Beverages - Soft Drinks","Beverages - Non-Alcoholic"]}},"IBM":{"company":{"symbol":"IBM","companyName":"International Business Machines Corporation","exchange":"New York Stock Exchange","industry":"Application Software","website":"http://www.ibm.com","description":"International Business Machines Corp offers a variety of IT services along with software, and hardware. It has operations in over 170 countries and provides planning, build, manage, and maintain IT infrastructure, platforms, applications, and services.","CEO":"Virginia M. Rometty","issueType":"cs","sector":"Technology","tags":["Technology","Information Technology Services","Application Software"]}},"V":{"company":{"symbol":"V","companyName":"Visa Inc.","exchange":"New York Stock Exchange","industry":"Credit Services","website":"http://www.corporate.visa.com","description":"Visa Inc is a payments technology company working to enable consumers, businesses, banks and governments to use fast, secure and reliable digital currency. The Company operates processing networks - VisaNet.","CEO":"Alfred F. Kelly","issueType":"cs","sector":"Financial Services","tags":["Financial Services","Credit Services"]}},"AXP":{"company":{"symbol":"AXP","companyName":"American Express Company","exchange":"New York Stock Exchange","industry":"Credit Services","website":"http://www.americanexpress.com","description":"American Express Co is a payments, network and travel company. It provides charge and credit card products, travel services, network services, stored value products, loans, and other products and services to businesses and individuals.","CEO":"Stephen J. Squeri","issueType":"cs","sector":"Financial Services","tags":["Financial Services","Credit Services"]}}}

I would like to pass a path string something like this:

ceoList = getPathValue(obj, '*.company.CEO');

...and the returned value would look like this:

Satya Nadella,Randall L. Stephenson,H. Lawrence Culp,Mark Zuckerberg,Timothy D. Cook,Mark G. Parker,James Robert Quincey,Virginia M. Rometty,Alfred F. Kelly,Stephen J. Squeri

IMPORTANT: the solution must remain ES5 compatible and specifically, IE11 compatible.

EDIT:

Unless I am missing something, this is NOT a duplicate. Please show a solution for the stated problem.

Excel Hero
  • 14,253
  • 4
  • 33
  • 40
  • What does the leading `{}.` mean? Does it mean that the parent of the `.company` key may be any object nested anywhere, or that it needs to be right below the top level, or what? – CertainPerformance Jan 28 '19 at 22:22
  • Yeah, not really sure what should actually go there for it to make sense. Though it is ugly, this might make more sense: '{}.{}.company.CEO' – Excel Hero Jan 28 '19 at 22:26
  • Essentially I just want to pass the function a string that identifies which list to return. For example, I might also want to get the comma separated list of company names or exchanges. Need some way to specify that... and of course for the function to parse the lists. – Excel Hero Jan 28 '19 at 22:29
  • just replace the `{}` with a star and pull only values from the result set of the duplicate target. – Nina Scholz Jan 28 '19 at 22:29
  • @NinaScholz Thanks for that. When I try the path string of: '*.company.CEO' it returns Undefined. – Excel Hero Jan 28 '19 at 22:32
  • i get an object with all pathes and values. – Nina Scholz Jan 28 '19 at 22:33
  • @NinaScholz Here is my CodePen. Am I doing something wrong? https://codepen.io/anon/pen/VgjqEM?editors=1011 – Excel Hero Jan 28 '19 at 22:35
  • you took another function. just take the first function `get`. – Nina Scholz Jan 28 '19 at 22:38
  • @NinaScholz Sorry, for being dense. I do not understand whay you mean. – Excel Hero Jan 28 '19 at 22:45
  • @NinaScholz I addressed the duplicate marking. Please read the EDIT. – Excel Hero Jan 29 '19 at 00:43
  • @NinaScholz I just read the other question you pointed to (at the top of this page). I don't think it is the same thing as what I'm after. And at any rate, I want/need to keep the getPathValue() function I have here on this page. I am only looking to enhance it for the wildcard/multiple results situation. From where I am sitting that other question you are directing me to is too far removed and it does not support the functionality already present in the function on this page. – Excel Hero Jan 29 '19 at 00:58
  • sorry, by all respects, you took a solution from another thread, requires a new functionality, and ask for a solution. according to the rules, you need to add a try for getting the wanted result. *actually the question is asking for code.* – Nina Scholz Jan 29 '19 at 07:41
  • the duplicate target offers this functionality, even more and gives you the opportunity to try yourself, look what is working, what not and what you need. then ask a new question, based on this. just another hint, the use of wildcards is quite different from getting a single value at the end of one path. the wildcard needs for every step an additional array and the result is moreover a cartesian product. – Nina Scholz Jan 29 '19 at 07:41
  • .@Excel Hero, as a workaround, you could try to use the JSON.stringify method to change the Json object array to a Json string, then, using the Regular expression and match method to get the value, the code and result [like this](https://i.stack.imgur.com/IdCMS.png). – Zhi Lv Jan 29 '19 at 09:58

0 Answers0