Brzozka
Brzozka

Reputation: 1

Change values of object with values of second object in Javascript

I have 2 files, one is an array of objects containing information about companies. Here is part of the first one :

[{
    "id": 1,
    "name": "PGE Elektrownia Turów Spółka Akcyjna",
    "address": {
        "coordinates": {
            "latitude": 50.945669,
            "longitude": 14.90811
        },
        "countryId": 0,
        "cityId": 1,
        "street": "Młodych Energetyków 12",
        "zip": "59-916"
    },
    "filterData": {
        "specialization": 0,
        "tags": [
            4
        ]
    },
    "website": "https://elturow.pgegiek.pl/",
    "email": "[email protected]",
    "phoneNumber": "75 773 49 00",
    "isPaid": "NIE",
    "logoFile": "",
    "socialMedia": {
        "facebook": "",
        "linkedIn": ""
    }
},
{
    "id": 2,
    "name": "Zakład Energetyki Cieplnej Sp. z o.o.",
    "address": {
        "coordinates": {
            "latitude": 51.264569,
            "longitude": 15.58923
        },
        "countryId": 0,
        "cityId": 2,
        "street": "Gałczyńskiego 51",
        "zip": "59-700"
    },
    "filterData": {
        "specialization": 0,
        "tags": [
            4
        ]
    },
    "website": "https://www.zec.boleslawiec.pl/",
    "email": "[email protected]",
    "phoneNumber": "75 732 08 43",
    "isPaid": "NIE",
    "logoFile": "",
    "socialMedia": {
        "facebook": "",
        "linkedIn": ""
    }
}]

As You can see these objects contain values that are reference to ID's in second object here's sample of the second one that contains filters values

{"city": [
    {
        "id": 0,
        "name": "Legnica",
        "searchMatchIds": [
            0,
            10,
            11,
            12,
        ],
        "childrenIds": []
    },
    {
        "id": 1,
        "name": "Bogatynia",
        "searchMatchIds": [
            1
        ],
        "childrenIds": []
    },
    {
        "id": 2,
        "name": "Bolesławiec",
        "searchMatchIds": [
            2,
            3,
            4,
            5,
            60,
            63
        ],
        "childrenIds": []
    }],
 "tags": [
    {
        "id": 0,
        "name": "programowanie",
        "searchMatchIds": [
            0,
            13,
            48,
            53,
            54
        ],
        "childrenIds": []
    },
    {
        "id": 1,
        "name": "web",
        "searchMatchIds": [
            0,
            10,
            18,
            20,
            45,
            52
        ],
        "childrenIds": []
    }]
}

And my question is what's the best way to replace Id values in companies for string values in filters If you want better representation of these files here they are 1. https://pastebin.com/TAqW1cFw 2. https://pastebin.com/BQWtFyUx I'm using Vue with vuex and already have both of these json files as states, just looking for function/logic that will be the most efficient in replacing values.

Upvotes: 0

Views: 44

Answers (1)

Jamiec
Jamiec

Reputation: 136104

If you're just interested in the name properties and it's efficiency you're after I might be tempted to pre-process the lookup data

const lookupInput = {"city":[{"id":0,"name":"Legnica","searchMatchIds":[0,10,11,12],"childrenIds":[]},{"id":1,"name":"Bogatynia","searchMatchIds":[1],"childrenIds":[]},{"id":2,"name":"Bolesławiec","searchMatchIds":[2,3,4,5,60,63],"childrenIds":[]}],"tags":[{"id":0,"name":"programowanie","searchMatchIds":[0,13,48,53,54],"childrenIds":[]},{"id":1,"name":"web","searchMatchIds":[0,10,18,20,45,52],"childrenIds":[]}]}

const lookups = Object.fromEntries(Object.entries(lookupInput).map( 
  ([key, data]) => ([key, data.reduce( (acc,i) => ({...acc, [i.id]: i.name}),{})])
));

console.log(lookups);

Then it just becomes a case of procesing your main input data and replacing the numbers with the names from the lookup data above

const lookupInput = {"city":[{"id":0,"name":"Legnica","searchMatchIds":[0,10,11,12],"childrenIds":[]},{"id":1,"name":"Bogatynia","searchMatchIds":[1],"childrenIds":[]},{"id":2,"name":"Bolesławiec","searchMatchIds":[2,3,4,5,60,63],"childrenIds":[]}],"tags":[{"id":0,"name":"programowanie","searchMatchIds":[0,13,48,53,54],"childrenIds":[]},{"id":1,"name":"web","searchMatchIds":[0,10,18,20,45,52],"childrenIds":[]}]}

const lookups = Object.fromEntries(Object.entries(lookupInput).map( 
  ([key, data]) => ([key, data.reduce( (acc,i) => ({...acc, [i.id]: i.name}),{})])
));

const input = [{"id":1,"name":"PGE Elektrownia Turów Spółka Akcyjna","address":{"coordinates":{"latitude":50.945669,"longitude":14.90811},"countryId":0,"cityId":1,"street":"Młodych Energetyków 12","zip":"59-916"},"filterData":{"specialization":0,"tags":[4]},"website":"https://elturow.pgegiek.pl/","email":"[email protected]","phoneNumber":"75 773 49 00","isPaid":"NIE","logoFile":"","socialMedia":{"facebook":"","linkedIn":""}},{"id":2,"name":"Zakład Energetyki Cieplnej Sp. z o.o.","address":{"coordinates":{"latitude":51.264569,"longitude":15.58923},"countryId":0,"cityId":2,"street":"Gałczyńskiego 51","zip":"59-700"},"filterData":{"specialization":0,"tags":[4]},"website":"https://www.zec.boleslawiec.pl/","email":"[email protected]","phoneNumber":"75 732 08 43","isPaid":"NIE","logoFile":"","socialMedia":{"facebook":"","linkedIn":""}}]

const result = input.map(i => ({
  ...i,
  address: {
     ...i.address,
     cityName: lookups.city[i.address.cityId],
     
  }  ,
  filterData: {
        ...i.filterData,
        tags: i.filterData.tags.map(t => lookups.tags[t])
     }
}));

console.log(result);

Note: The above replace all tags with undefined but that is because your lookup data does not have the right id (4) - if it did then it should work.

Upvotes: 1

Related Questions