0

Trying to write logic if object path are same it should create one object in the array with name comma separated, How to use reduce to achieve this task ?

main.js

 var data = [
{id: "param", name: "IGetRefillMultiPlanParam", path: "@combinator-interfaces/dist/drug/GetRefills/GetRefills.Model.ts"}
{id: "details", name: "IRefills", path: "@combinator-interfaces/dist/drug/GetRefills/GetRefills.Model.ts"}
{id: "header", name: "IHeader", path: "@combinator-interfaces/dist/common/Header.Model.ts"}
]

    function buildInterface (data) {
           var interfaceArray = [];
           data.reduce(function(acc,arr){
            var filePath = acc.path.replace('.ts','');
            var imports  = data.map(d => d.name).join(', '); 
                if(acc.path.includes(arr.path)) {
                interfaceArray.push(`import { ${imports} } from '${filePath}';\n\n`);
                }
            });

            return interfaceArray;

       }

Expected Result

[
    'import { IGetRefillMultiPlanParam, IRefills} from "@combinator-interfaces/dist/drug/GetRefills/GetRefills.Model"',
    'import { IHeader } from "@combinator-interfaces/dist/common/Header.Model"'
]
5
  • What's wrong with your approach? Why did you ask this question? Commented Oct 1, 2019 at 17:05
  • @Andreas its throwing error i Uncaught TypeError: Cannot read property 'path' of undefined Commented Oct 1, 2019 at 17:12
  • I don't think you know how to use reduce yet. Also, can you explain your algorithm in English? What exactly are you trying to do? Commented Oct 1, 2019 at 17:20
  • @slider expected result is there to understand what i am trying to achieve , i know i don't have knowledge about reduce trying to learn, basically if objects have same path it should create one import object with the names in the imports. Commented Oct 1, 2019 at 17:22
  • Yes, I understand the problem statement. But I'm not sure if I understand your approach to solving it based on the code you've pasted. That's why it may help in clarifying your high level approach in English. Commented Oct 1, 2019 at 17:26

1 Answer 1

1

You were using reduce wrong. Take a look at this and if you have any questions ask.

const data = [
  {
    id: "param",
    name: "IGetRefillMultiPlanParam", 
    path: "@combinator-interfaces/dist/drug/GetRefills/GetRefills.Model.ts"
  },
  {
    id: "details",
    name: "IRefills", 
    path: "@combinator-interfaces/dist/drug/GetRefills/GetRefills.Model.ts"
  },
  {
    id: "header",
    name: "IHeader", 
    path: "@combinator-interfaces/dist/common/Header.Model.ts"
  },
];

function buildInterface (data) {
  const parsed = data.reduce((map, importing) => {
    if (!map[importing.path]) {
      map[importing.path] = [];
    }
    const list = map[importing.path];
    
    list.push(importing.name);
    
    return map;
  }, {});
  console.log('parsed', parsed);
  
  return Object.entries(parsed).map(([importPath, toImport]) => {
    return `import { ${toImport.join(', ')} } from '${importPath.replace('.ts','')}'`;
  });
}
console.log(buildInterface(data));

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

2 Comments

Thank you Boss solved my problem! Still scratching my head how did you do that :) will figure it out. thanks again
The way the reduce function is used here is to me the most common. This is the most readable way to convert array to object of some sort.

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.