Search code examples
javascriptarrayslodash

Using lodash to map complex JSONs


I'm trying to map data from one format to another and I'm having some hard time.

FIRST ONE

from:

  types: {
      one: [0.0954835843,6.054385,84.40238],
      two: [0.0954835843,6.054385,84.40238],
      three: [0.0954835843,6.054385,84.40238]
    },

to

    types: [
      [0.0954835843,6.054385,84.40238 , 'one'],
      [0.0954835843,6.054385,84.40238, 'two'],
      [0.0954835843,6.054385,84.40238, 'three']
    ],

SECOND ONE match with ID, by getting data from two objects

          types:[{
            id:1, 
            data:[
                    [
                        'temp',
                        '46%',
                        '14.00',
                        '1388.68561604',
                        '1388.68561604',
                        '2018-02-12 18:21'

                    ],
                    [
                        'nottemp',
                        '46%',
                        '14.00',
                        '1388.68561604',
                        '1388.68561604',
                        '2018-02-12 18:21'

                    ],
               ] 
            },
            {
            id:2, 
            data:[
                    [
                        'temp',
                        '46%',
                        '14.00',
                        '1388.68561604',
                        '1388.68561604',
                        '2018-02-12 18:21'

                    ],
                    [
                        'nottemp',
                        '46%',
                        '14.00',
                        '1388.68561604',
                        '1388.68561604',
                        '2018-02-12 18:21'

                    ],
               ] 
            }]






data: [
        {
          id: 1,
          name: 'sibling',
        },
        {
          id: 2,
          name: 'parent',
        },
     ]

and end up with all the items from types.data in format like:

        [
                'temp',
                '46%',
                '14.00',
                '1388.68561604',
                '1388.68561604',
                '2018-02-12 18:21'
                'parent' //or sibling, depends on id

            ]

I actually have no idea how I can achive that with some mapping or filtering. It's crazy hard for me to achive it without tons of modifiers and without creating few additional arrays in the process. Is there any way to optimize it?

EDIT for the second issue I have:

   mapElement: function(){
        let that=this;
        return _.map(this.getTypesData,function(element){
          let data=[];
          let pairName = that.getFamily(element.id); //todo: matching id of Type with id of object with names
          element.data.forEach(function(element){
            data.push([
              pairName,
              element[0],
              element[1],
              element[2],
            ]);
          });
          return data;
        });
      }

Solution

  • the 1st question:

    super easy with _.map:

    // Also handle case value is not array
    types = _.map(types, (val, key) => _.isArray(val) ? v.concat(key) : [val].concat(key)); 
    

    the 2nd question:

    I optimized for performance:

    //clone to new object to avoid modifying original data
    const cloneData = _.cloneDeep(data);
    
    types = _.map(types, (val, key) => {
        const index = _.findIndex(cloneData, { id: val.id});
        val.data = _.map(val.data, (v)=> _.isArray(v) ?v.concat(cloneData[index].name) : [v].concat(cloneData[index].name));
        //remove when found id to shorten the array
        // if the types[].id is not unique, remove this
        cloneData.splice(index,1);
    
        return val;
    })