skip to Main Content

I have a object obj1 which has mapping information which is present in other as values vals. I am able to create flat object structure but recursive is not working so require help on this

const obj1 = [
  {
    mapping: "cities",
  },
  {
    mapping: "category",
    children: {
      mapping: "type",
      children: {
        mapping: "otherType",
      },
    },
  },
  {
    mapping: "age",
  },
];

const vals = [
  {
    mapping: "category",
    values: [{}],
  },
  {
    mapping: "type",
    values: [{}],
  },
  {
    mapping: "otherType",
    values: [{}],
  },
  {
    mapping: "cities",
    values: [{}],
  },
  {
    mapping: "age",
    values: [{}],
  },
];


I want expected data in below fromat


const exectedData = {
    cities: {
        values: [{}],
    },
    category: {
        values: [{}],
        children: {
            type: {
                values: [{}],
                children: {
                    otherType: {
                        values: [{}],
                    }
                }
            }
        }
    },
    age: {
        values: [{}]
    }
}

2

Answers


  1. You could transform your values to an object for faster update value finding and make a simple recursion:

    const obj = [
      {
        mapping: "cities",
      },
      {
        mapping: "category",
        children: {
          mapping: "type",
          children: {
            mapping: "otherType",
          },
        },
      },
      {
        mapping: "age",
      },
    ];
    
    const vals = [
      {
        mapping: "category",
        values: [{}],
      },
      {
        mapping: "type",
        values: [{}],
      },
      {
        mapping: "otherType",
        values: [{}],
      },
      {
        mapping: "cities",
        values: [{}],
      },
      {
        mapping: "age",
        values: [{}],
      },
    ];
    
    const map = vals.reduce((r, {mapping, ...item}) => (r[mapping] = item, r), {});
    
    const update = obj => {
      if(Array.isArray(obj)) return obj.forEach(update);
      if(typeof obj !== 'object') return;
      Object.values(obj).forEach(update);
      Object.assign(obj, map[obj.mapping]);
    };
    
    update(obj);
    console.log(obj);
    Login or Signup to reply.
  2. Try this

    const arr = [
      {
        mapping: "cities",
      },
      {
        mapping: "category",
        children: {
          mapping: "type",
          children: {
            mapping: "otherType",
          },
        },
      },
      {
        mapping: "age",
      },
    ];
    
    const rec = ({ mapping, children }) =>
      children
        ? {
            [mapping]: {
              values: [{}],
              children: { ...rec(children) },
            },
          }
        : {
            [mapping]: {
              values: [{}],
            },
          };
    
    const tmp = arr.reduce((acc, item) => {
      if (item.mapping) acc = { ...acc, ...rec(item) };
      return acc;
    }, {});
    
    console.log(tmp);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search