Ernesto Pérez
Ernesto Pérez

Reputation: 51

How to insert data from one object to another with different structure to generate third object

I'm trying to extract data from an object and insert this data into another object, but the two objects have different structures. And its very large object, more than 3000 lines.

I need to check in object1 if dictionary[0].key.mcp[0] exists and if it exists in object2 conv[0].mcp I need to insert the 4 languages: German, Swedish, Chinese and Japanese in to the object1 dictionary[0].dicts and if it doesn't exist, then do nothing

I think maybe with a for loop I will be able to accomplish task but the problem is that in some cases below the key the value mcp changes for another.

Object 1

dictionary = [
  {
    "key": {
      "mcp": [
        "mcp.empty"
      ]
    },
    "dicts": {
      "english": "",
      "german": ""
    },
    "context": null,
    "tags": [],
    "edited": "2016-10-27T15:41:31.000Z"
  },
  {
    "key": {
      "mcp": [
        "ui.controls.plasmacontrol.feeder"
      ]
    },
    "dicts": {
      "english": "Feeder",
      "german": "Förderer"
    },
    "context": null,
    "tags": [],
    "edited": "2016-10-27T15:41:31.000Z"
  },

Object 2

var conv = [
    {
        "mcp": "mcp.empty",
        "english": "",
        "german": "",
        "Swedish": "",
        "chinese": "",
        "japanese": ""
    },
    {
        "mcp": "ui.controls.plasmacontrol.feeder",
        "english": "Feeder",
        "german": " Förderer ",
        "Swedish": "Transportör",
        "chinese": "送粉器",
        "japanese": "粉末供給装置"
    },
    {
        "mcp": "ui.controls.plasmacontrol.feeder1",
        "english": "Feeder 1",
        "german": "Förderer 1 ",
        "Swedish": "Transportör 1",
        "chinese": "1号送粉器",
        "japanese": "粉末供給装置1"
    },

The expected result is an object with all the data inserted from object1 to object2.

Object 3 (Expected result)

 dictionary = [
      {
        "key": {
          "mcp": [
            "mcp.empty"
          ]
        },
        "dicts": {
         "english": "",
            "german": "",
            "Swedish": "",
            "chinese": "",
            "japanese": ""
        },
        "context": null,
        "tags": [],
        "edited": "2016-10-27T15:41:31.000Z"
      },
      {
        "key": {
          "mcp": [
            "ui.controls.plasmacontrol.feeder"
          ]
        },
        "dicts": {
          "english": "Feeder 1",
            "german": "Förderer 1 ",
            "Swedish": "Transportör 1",
            "chinese": "1号送粉器",
            "japanese": "粉末供給装置1"
        },
        "context": null,
        "tags": [],
        "edited": "2016-10-27T15:41:31.000Z"
      },

Until now I have this and is returning some errors

import dictionary from "./dictionary.mjs";
import conv from "./converted.mjs";
import _ from "lodash";
import fs from "fs";
import util from 'util';


const newObject = _.map(dictionary, dictionaryItem => {
   const key = _.get(dictionaryItem, "key.mcp[0]", "")
   const isPresent = _.find(conv, { "mcp": key })
   if(isPresent) {
     /*  console.log('se hizo') */
      return { ...dictionaryItem, dicts: { 
             "english": isPresent.english,
            "german": isPresent.german,
            "Swedish": isPresent.Swedish,
            "chinese": isPresent.chinese,
            "japanese": isPresent.japanese
         } 
      }
  } else {
/*    console.log('no') */
         return dictionary
  }
})  

try {
   const data = fs.writeFileSync('./final.js', util.inspect(newObject, {showHidden: false, depth: null}));
   //file written successfully
 } catch (err) {
   console.error(err)
 }

Upvotes: 0

Views: 242

Answers (1)

Ashok
Ashok

Reputation: 2932

Use lodash First import where you want to perform the operation

import _ from "lodash"
  or
const _ = require("lodash")

And

const newObject = _.map(dictionary, dictionaryItem => {
   const key = _.get(dictionaryItem, "key.mcp[0]", "")
   const isPresent = _.find(conv, { "mcp": key })
   if(isPresent) {
      return { ...dictionaryItem, dicts: { 
             "english": isPresent.english,
            "german": isPresent.german,
            "Swedish": isPresent.Swedish,
            "chinese": isPresent.chinese,
            "japanese": isPresent.japanese
         } 
      }
  } else {
         return dictionary
  }
})  

OMG you got your desire result

Upvotes: 1

Related Questions