0

I have this object and a I need to get the value of property name to assign it to a variable so that I can use it for each option in a menu. Here is the data returned:

“parentArrayOfObjects”: [
    {
     “data”: {
        “current”: {
           “id”: 0, 
           “name”: “Winter”
         }   
      }
    },
    {
      “data”: {
         “current”: {
            “id”: 0, 
            “name”: “Spring”
          }   
        }
     }    
 ]

Using the above, I’m getting the same value for the data.current.name property for each menu at index 0, and that isn’t what I want. Please tell me what I'm doing wrong. Thanks.

// returns same name in all menus
var getCurrentChildPropertyName = (function(){
    for (var value in parentArrayOfObjects) {
         var currentChildProperty=parentArrayOfObjects[value].data.current;
         if( currentChildProperty !== null){                                                        
             return currentChildProperty.name;
         }
    }
})();

// returns same name in all menus
var getCurrentChildPropertyName = (function(){
    for ( var i = 0; i < parentArrayOfObjects.length; i++) {
          var currentChildProperty = parentArrayOfObjects[i].data.current;
          if( currentChildProperty !== null){
              return currentChildProperty.name;
          }
     }
})();


// Use different returned name in each menu (ExtJs3)
     dataStore.insert(0, [new Ext.data.Record({
                 id: 0,                                                    
                 name: getCurrentChildPropertyName
     })
1
  • You're returning when you find a match, which will end the execution of the function and return the value immediately. Commented Mar 11, 2019 at 23:03

1 Answer 1

1
const x = {'parentArrayOfObjects': [
  {
   'data': {
      'current': {
         'id': 0,
         'name': 'Winter'
       }
    }
  },
  {
    'data': {
       'current': {
          'id': 0,
          'name': 'Spring'
        }
      }
   }
]}

const results = x.parentArrayOfObjects.map((item) => item.data.current.name);

console.log(results); //[ 'Winter', 'Spring' ]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I'll try this, but if this is returning an array of names all at once, that's the problem I'm already having. What I need is, After I get the names in an array, how do I get the unique name (which is why the variable is Singular not Plural) so that I can assign that unique name to it's specific menu item name (as in the datastore.insert method)? For example: menu1.index0.name = Winter, menu2.index0.name = Spring and so on.

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.