1

Fellow programmer here, for some reason I have spent two days but I cannot do this properly so help me out. I have an object with arrays for each key like this:

var arraysObject = {
    "North American": [
        "CONUS",
        "NAMER",
        "CONUS/AK",
        "US-NC",
        "US-NE",
        "US-NW",
        "US-SC",
        "US-SE"
    ],
    "Oconus": [
        "ALASKA",
        "GUAM",
        "HAWAII",
        "POLAR",
        "US-SAMOA"
    ],
 }

I have a list of areas that have been selected, like the following

var listOfAreas = ["CONUS", "GUAM","US-SAMOA"]

I want to create the following object:

var result = [{
        "North American":"CONUS",
        "Oconus": "GUAM"
        },
        {
        "North American": "",
        "Oconus":"US-SAMOA"
        }]
        
            

Notice how it created a new object only when that last one's key is full and fills the object if possible. in my implementation it creates an object for each individual string and assigns it to the key using then pushes it using .map This doesn't work. I want it to fill each object accordingly and create an new object only when it is necessary. Appreciate your help. Best, A dummy.

7
  • What happens with two "North American" results like [CONUS, NAMER]? Because it seems the "unique" values should be come the keys, not the other way around. Commented Feb 11, 2021 at 21:34
  • I'm not able to understand the data structure you are trying to create. are you able to clarify? Commented Feb 11, 2021 at 21:34
  • the data structure is an array of objects, basically an inverse of the first object with only the strings in the selected array. This is used to create a table with bootstrap and needs to be in that format. Commented Feb 11, 2021 at 21:47
  • @RandyCasburn CONUS was the only selected string in the list of arrays that was in the North America array Commented Feb 11, 2021 at 21:48
  • @TadewosBellete - yes, I understand what you provided as an example. My question has to do with different input arrays that may contain two elements from North America. Can that ever happen in your app? Commented Feb 11, 2021 at 21:52

1 Answer 1

1

You could take an object for keeping track of the indices for the keys and take a helper object for the reverted value/key relation.

const
    arraysObject = { "North American": ["CONUS", "NAMER", "CONUS/AK", "US-NC", "US-NE", "US-NW", "US-SC", "US-SE"], "Oconus": ["ALASKA", "GUAM", "HAWAII", "POLAR", "US-SAMOA"] },
    keys = Object.fromEntries(Object.entries(arraysObject).flatMap(([k, a]) => a.map(v => [v, k]))),
    EMPTY = Object.fromEntries(Object.keys(arraysObject).map(k => [k, ''])),
    listOfAreas = ["CONUS", "GUAM","US-SAMOA"],
    indices = {},
    result = [];

listOfAreas.forEach(value => {
    const key = keys[value];
    indices[key] ??= 0;
    (result[indices[key]++] ??= { ...EMPTY })[key] = value;
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

6 Comments

But hold on, it is giving me duplicate values during implementation.
where do you get duplicates? the values should be unique.
oh debugging showed that the slected list has duplicates for some reason. .map giving duplicates some how. not your code lol
it gets the key from the keys object, assigns zero to the indices object with this key or keeps the value if not null or undefined` (logical nullish assignment ??=, this is used later with a spreading of an object with only emtpty strings as values).
in the last line the assignment takes place by using the index from indices (where the index is incremented), a check is made for null or undefined, the object is in this case assigned and finally the value goes to its right place with index and key.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.