-1

Not sure if my title describes what I want to do correctly. Basically, I want a function that extracts properties from objects containing objects. I am going to need to loop through various arrays containing many objects of the same class and extract specific values.

myarray1[
0:
    object1 = { 
       objectProp1: {
           objectProp1Prop1:"Hello",
           objectProp1Prop2:"Goodbye",
           objectProp1Prop3:{
               objectProp1Prop3Prop1: "Come here",
               objectProp1Prop3Prop2: "Go away"
           },
        },
        objectProp2: "Yo",
        objectProp3: "Seeya",
    }
1:
    object2 = { same as object1 but with other property values }
];

myarray2[
0: { different type of object with a different set of nested properties that the function can extract }
1: { idem }
];

function extractProperty(objectArray, property) {
    //How do I write this code?
    propertyvalue = objectArray.property;

    return propertyvalue;
}

extractProperty(myarray1[0], object.objectProp3) = "Seeya"
extractProperty(myarray1[0], object.objectProp1.objectProp1Prop1) = "Hello"
extractProperty(myarray1[0], object.objectProp1.objectProp1Prop3.objectProp1Prop3Prop1) = "Come here"

In the final code the function needs to be able to loop through all the array keys and create an array list containing the chosen property from every object in the original array, but that I can manage. It's the sending of the specific property that needs to be extracted from the objects in the array into the function that I have no idea how to do.

Is there a generalised way to send a "path" of properties into a function and then use it there? How?

Thanks for your help!

coding_pianist
  • 71
  • 2
  • 10
  • No, because I don't know how to send the specific property that needs to be extracted into the function and use it from there. Wasn't that clear in the question? – coding_pianist Nov 04 '17 at 01:33
  • Sorry, my above comment was a response to something that has since been deleted by either author or mods. – coding_pianist Nov 04 '17 at 01:40

2 Answers2

-1

You could try recursion:

    object1 = { 
       objectProp1: {
           objectProp1Prop1:"Hello",
           objectProp1Prop2:"Goodbye",
           objectProp1Prop3:{
               objectProp1Prop3Prop1: "Come here",
               objectProp1Prop3Prop2: "Go away"
           },
        },
        objectProp2: "Yo",
        objectProp3: "Seeya",
    };

object2 = {
    objectProp1: 'test1',
  objectProp2: 'test2'
}

var myArray = [object1, object2];

function getProp(objArray, prop) {
    for(var key in objArray) {
        if (key == prop) 
            return objArray[key];
        if (typeof objArray[key] == 'object')
            return getProp(objArray[key], prop);
    }
}

//test
document.getElementsByTagName('h1')[0].innerHTML = getProp(myArray[0],'objectProp1Prop3Prop1');

I added a Fiddle for you to try it: https://jsfiddle.net/afabbro/vrVAP/

user3362334
  • 1,980
  • 3
  • 26
  • 58
-1

Looks like an assignment to me. So I won't give you the code but will explain the approach.

  1. First you need to pass the property names as a string
  2. In your function you need to split the string based on the delimiter, like .
  3. Keep a reference of current object
  4. Then iterate on all the property names that you got from #2
  5. Fetch current property name from current object and replace current object with the returned value.
  6. return current object at the end.

Note: you need to add some validations in between. I've skipped those for you to explore ;)

Vivek Athalye
  • 2,974
  • 2
  • 23
  • 32
  • Thank you for your reply. It's not an assignment, but a private project I'm doing as a hobby in order to teach myself to code. I will try your approach and see if I can make sense of it! – coding_pianist Nov 06 '17 at 18:58