1

So I've been trying to find a solution to this for a little while with no luck.

const nameTest = 'testName';
const test = {
   RANDOM_ONE: {
       NAME: 'testName',
       SOMETHING: {...}
   },
   RANDOM_TWO: {
       NAME: 'Name',
       SOMETHING: {...}
   }
}

Is there any simple, easy way where I can compare the nameTest and the NAME key without knowing what the RANDOM_X is in order to access NAME?

1

5 Answers 5

2

You can use Object.keys() to get the array of all the keys. Then loop through the array to check the property:

const nameTest = 'testName';
const test = {
   RANDOM_ONE: {
       NAME: 'testName',
       SOMETHING: {}
   },
   RANDOM_TWO: {
       NAME: 'Name',
       SOMETHING: {}
   }
}
let testKeys = Object.keys(test);
testKeys.forEach(function(k){
  console.log(test[k].NAME == nameTest);
});

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

Comments

0

You can use a for ... in loop:

for (let key in test) {
  if (test[key].NAME === nameTest) {
    // do something
  }
}

Comments

0

I hope we know that 2 levels down into test is your object. You could write a function, to compare the name key.

function compare(obj, text){
 for(let x in obj){
   if(obj.x.name == text) return true;
   else ;
 }
}

Then call the function with your object and the string.

let a = compare(test, nameTest);

Note: this would compare the object to only ascertain if it contains the nameTest string.

Comments

0
var obj= test.filter(el){
    if(el.NAME==nameTest)
    {
        return el;
    }
 }
var x= obj!=null?true:false;

1 Comment

While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
0

You could use find.

The find method executes the callback function once for each index of the array until it finds one where callback returns a true value. If such an element is found, find immediately returns the value of that element. Otherwise, find returns undefined.

So it is more memory efficient, than looping over the whole object with forEach, because find returns immediately if the callback function finds the value. Breaking the loop of forEach is impossible. In the documentation:

There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.

1. If you want to get the whole object

var nameTest = 'testName';
var test = {
   RANDOM_ONE: {
       NAME: 'testName',
       SOMETHING: {}
   },
   RANDOM_TWO: {
       NAME: 'Name',
       SOMETHING: {}
   }
};

function getObjectByNameProperty(object, property) {
    var objectKey = Object.keys(object)
        .find(key => object[key].NAME === property);
    return object[objectKey];
}

var object = getObjectByNameProperty(test, nameTest);
console.log(object);

2. If you just want to test if the object has the given name value

var nameTest = 'testName';
var test = {
   RANDOM_ONE: {
       NAME: 'testName',
       SOMETHING: {}
   },
   RANDOM_TWO: {
       NAME: 'Name',
       SOMETHING: {}
   }
};

function doesObjectHaveGivenName(object, nameValue) {
    var objectKey = Object.keys(object)
        .find(key => object[key].NAME === nameValue);
    return objectKey ? true : false;
}

console.log( doesObjectHaveGivenName(test, nameTest) );

Comments

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.