Ananth Vivek
Ananth Vivek

Reputation: 73

Javascript map operator

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.

Upvotes: 0

Views: 184

Answers (3)

rashi jain
rashi jain

Reputation: 484

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.

Upvotes: 0

imgnx
imgnx

Reputation: 789

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)

Upvotes: 0

Anthony
Anthony

Reputation: 6482

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);

Upvotes: 1

Related Questions