GPP
GPP

Reputation: 2315

Creating an array of objects using an array of each object's name

I feel tantalizingly close here. Scenario: I have a JSON sent in a message that looks something like this:

{"objects": {
    "object_name1":{ 
        "name":"object_name1",
        "otherData":"some other data"
    },
    {"object_name2":{ 
        "name":"object_name2",
        "otherData":"some more data"
    }
}}

As you can see, the objects are not in an array. I created an array containing each Object's string name using the Object.keys function and then created another array which contains the objects that I want using an if statement, which leads to my current question:

How can I create an array of objects using the string values (the object keys) contained in the array I have created?

I've tried something like this:

filteredKeyArray = ['object_name1','object_name2'];
newObjArray = [];
for(i in filteredKeyArray){
    for(key in objects){
    newObjArray.push(objects[i[key]]);
    }
}

But the newObjArray is just displaying all undefined. Ideally the newObjArray displays something like this:

[{"object_name1":{...}},{"object_name2":{...}}]

Thoughts?

Upvotes: 0

Views: 2086

Answers (1)

Happy Machine
Happy Machine

Reputation: 1163

If i understand what you want to do correctly you want to create an iterable array containing the child objects of objectName?

I would do it as below, if you use the array prototype map function you can in one line of code iterate over all the keys of the object using Object.keys(target) and return the properties you want (in this case the inner child objects) as array elements via map.

strangeObject = {
    "objects":{
        "objectName_1":{
            "name":"x",
            "otherData":"some other data"
        },
        "objectName_2":{
            "name":"y",
            "otherData":"some other data"
        }
    }
}
const arrayOfObjects = Object.keys(strangeObject.objects).map(key => strangeObject.objects[key])
console.log(arrayOfObjects)
// logs [ { name: 'x', otherData: 'some other data' },
// { name: 'y', otherData: 'some other data' } ]

mapped as in your example, method 1

const filteredKeyArray = ['x','y'];
const remappedArray = Object.keys(strangeObject.objects).map((key, i) => { 
    return { [filteredKeyArray[i]] : strangeObject.objects[key]}
})

console.log(remappedArray)
// logs [ { x: { name: 'x', otherData: 'some other data' } },
// { y: { name: 'y', otherData: 'some other data' } } ]

iterating over the filteredKeyArray, method 2

const filteredIterator = filteredKeyArray.map((key, i) => {
    return { [key]: strangeObject.objects[Object.keys(strangeObject.objects)[i]]}
})

console.log(filteredIterator)
// logs [ { x: { name: 'x', otherData: 'some other data' } },
// { y: { name: 'y', otherData: 'some other data' } } ]

Note that these solutions assume that you will have a key in your filteredKeyArray to match the child object to as the map iterates through, you may want to add a check to check if the length of the filteredKeyArray is longer than the map iterator, or if filteredKeyArray[i] is undefined if using method 1. If using method two you may need to check for undefined before referencing Object.keys(strangeObject.objects)[i] as you may have less objects that fiteredKeyArray keys.

Upvotes: 3

Related Questions