0

I have an object like this

[
  {
    _id: '5ef34e92858bff53bcf69e11',
    factors: [ {factor_id:'factor_id1',calmode:'calmode1',desc:'desc1',webserv:'webserv1',approach:'approach1'},
               {factor_id:'factor_id2',calmode:'calmode2',desc:'desc2',webserv:'webserv2',approach:'approach2'}, 
               {factor_id:'factor_id3',calmode:'calmode3',desc:'desc3',webserv:'webserv3',approach:'approach3'} 
             ],
    clientId: 'Company1',
    module: 'Mod1',
    __v: 0
  },
  {
    _id: '5ef350c9c1acd61e58ef9d08',
    factors: [ {factor_id:'factor_id4',calmode:'calmode4',desc:'desc4',webserv:'webserv4',approach:'approach4'}, 
               {factor_id:'factor_id5',calmode:'calmode5',desc:'desc5',webserv:'webserv5',approach:'approach5'}
             ],
    clientId: 'Company1',
    module: 'Mod2',
    __v: 0
  }
]

I want to create a final list like below

_id, ClientId,module,factor_id,calmode,desc,webserv,approach

I am trying to use map operator within another map operator but its not coming out properly. Any help would be appreciated.

const tmpFacLst = FacExists.map((module) => {
        const Factor = {
          module_id: module._id,
          module: module.module,
        };
        return Factor;
        /*const Fac = module.factors.map((factor)=>{
          const FactorDtl = {
            factor_id:factor._id,
            factor_desc: factor.desc
          }
          return FactorDtl;
        })*/
      });

Update: I am able to achieve using loop

const result = [];
  FacExists.forEach((item) => {
    const Factors = item.factors;
    Factors.forEach((Subitem) => {
      const Facobj = {
        _id: item._id,
        ClientId: item.clientId,
        module: item._id,
        factor_id: Subitem._id,
        calmode: Subitem.calmode,
        desc: Subitem.desc,
        webserv: Subitem.webserv,
      };
      result.push(Facobj);
    });
  });

I want to know is there any better way of doing this without looping.

3
  • 1
    Your first snippet is broken, it appears you don't have the data for Factor. Could you please paste that in? Commented Jun 24, 2020 at 17:55
  • not coming out properly - how's it coming it out exactly? Please add it in the question. Commented Jun 24, 2020 at 17:55
  • Now I have expanded factors object. I am getting [undefined,undefined] if I return it within the second map operator. If return within the first map operator I am getting the Factor array as it is. I don't want to loop. Please let me know if you need more info. I will also try other options from my end. Commented Jun 25, 2020 at 2:33

3 Answers 3

1

An approach like this should work:

const items = [
  {
    _id: "5ef34e92858bff53bcf69e11",
    factors: [
      {
        factor_id: 2,
        calmode: "cal",
        desc: "something",
        webserv: "10.0.0.0",
        approach: "forwards",
      },
    ],
    clientId: "Company1",
    module: "Mod1",
    __v: 0,
  },
  {
    _id: "5ef350c9c1acd61e58ef9d08",
    factors: [
      {
        factor_id: 3,
        calmode: "cal",
        desc: "something",
        webserv: "10.0.0.1",
        approach: "forwards",
      },
    ],
    clientId: "Company1",
    module: "Mod2",
    __v: 0,
  },
];

const result = [];
items.forEach((item) => {
  const { factors, __v, ...rest } = item;
  result.push(...factors.map((factor) => ({ ...factor, ...rest })));
});

console.log(result);

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

1 Comment

thanks for the answer. I am getting lot of meta data with this answer.
0

First, you need to clean your question up a bit because you have the Object keyword / class listed as elements of your factors array, which you call an "object". You should include those objects in your snippets.

let notAnObj = [
  {
    _id: '5ef34e92858bff53bcf69e11',
    factors: [ {_id: 1234, desc: 'bob loblaw'}],
    clientId: 'Company1',
    module: 'Mod1',
    __v: 0
  },
  {
    _id: '5ef350c9c1acd61e58ef9d08',
    factors: [],
    clientId: 'Company1',
    module: 'Mod2',
    __v: 0
  }
]
   console.log(notAnObject)

let arr= [
  {
    _id: '5ef34e92858bff53bcf69e11',
    factors: [ {_id: 1234, desc: 'bob loblaw'}],
    clientId: 'Company1',
    module: 'Mod1',
    __v: 0
  },
  {
    _id: '5ef350c9c1acd61e58ef9d08',
    factors: [],
    clientId: 'Company1',
    module: 'Mod2',
    __v: 0
  }
]

      const tmpFacLst = arr.map((obj) => {
        return {
          _id: obj._id, 
          ClientId: obj.clientId,
          module: obj.module,
          factors: obj.factors.map(factor => {
            return {
              _id: factor._id,
              desc: factor.desc,
            }
          }),
          calmode: undefined,
          webserv: undefined,
          approach: undefined
        };
      });
      console.log(tmpFacLst)

2 Comments

with this I am getting the object which is similar to my original object
@AnanthVivek You're missing some sort of reduction (.reduce) or something in your logic. .map is designed to return an array of the same size containing whatever values you place in the object that is returned. I don't understand what the goal is. What does the final schema look like?
0

you can do it like this

 const finalResult = FacExists.reduce((aggregator,fact) => {
      let result =   fact.factors.map(fac=>{
          return {
             _id: fact._id,
             clientId: fact.clientId,
             module: fact.module,
             
             ...fac
          }})
aggregator = [...aggregator,...result];
return aggregator
  },[]);

you will get the desired result in the "finalResult" variable.

4 Comments

thanks for the answer. I am getting two undefined objects.
can you please share the result which you are getting
Here is the result when I printed the object in console [ undefined, undefined ]. I think its expected as nothing is returned from the outer map iteration.
@AnanthVivek I have edited my answer. This will work.

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.