Hello.
My question is very basic but I can not find/understand the answer anywhere.
How check if the name-value "name": value in an object.
I know that with Object.keys() method we can checking the names of the properties.
I know too that with Object.name or Object["name"] we can see the value of the "named-name" property.
But if I have a singleton {"name": value} and want to check many large object for it, is it a direct method?
For example object 1 is {'a': 'lphabet', 'b': "cycle'} and I search 'a': 'bsurd'.
Tthank you in advance

Recommended Answers

All 10 Replies

You cannot, in any fast way, compare objects directly in JS.

The only way to check values is to iterate through the object and compare values directly for what you are looking for (or, as you pointed out, simply check the value directly). Since all an object really is on the inside is a giant hash map, lookups like this are very fast - as long as you know the key.

Your example is a little vague on what you are trying to accomplish. What is your end goal, and maybe someone can point you in the right direction.

To iterate, in case that was your question, you would do something like this:

//I am looking for "two"
var oObj = {a: "one", b:"two", c:"three"};
for (var cParam in oObj)
{
    if (oObj[cParam] == "two")
        {
              console.log("Found!");
              break;
         }
}

@ryantroop
Very good introduction for me.
Here is an attempt, an unfruitful attempt, to write a program. The important is to obtain a coupled name-value.

//To iterate, in case that was your question, you would do something like this:

//I am looking for "two"

var myFriends = [
{firstName: "Bertrand", lastName: "Michel"}, 
{firstName: "Michel", lastName: "Bertrand"}, 
{firstName: "Henri", lastName: "Michel"}, {}
];

var myFamily = {lastName: "Michel"}
var myFamilyMembers = [];
//I want to build a function that permit to
// obtain arrays of members of 
// a same family. e. g.  
//var familyMichel = [{{firstName: "Bertrand", lastName: "Michel"}, 
//{firstName: "Henri", lastName: "Michel"}}]
for (i = 0; i < myFriends.length; i ++) {// 1eP
for (var individu in myFriends[i]) {//2eP
// the iteration will be in the names: "firstName", "lastName"

    if (individu['lastName'] == "Michel")
        {//3eP
              myFamilyMembers.push(myFriends[i]);

         }//3eP closed

}//2eP closed
 console.log(myFamilyMembers);
}//1eP closed

From your myFriend variable data structure, you don't need to iterate it that way. It is already an associated array (as Object). You simply call for the value using lastName key.

for (var friend in myFriends) {
   if (friend["lastName"]=="Michel") { myFamilyMembers.push(friend); }
}

@Taywin Thank's.
First, I know from now, that an object is implicitely associated to a list of they names. It's allready a progress.
But something is wrong in the incrementation of the myFamilyMembers array.
Here is the snippet:

var myFriends = [
{firstName: "Bertrand", lastName: "Michel"}, 
{firstName: "Michel", lastName: "Bertrand"}, 
{firstName: "Henri", lastName: "Michel"}, {}
];
var myFamily = {lastName: "Michel"};
var myFamilyMembers = [];
for (var friend in myFriends) {// 1eP
    if (friend.lastName == "Michel")
        {//2eP
              myFamilyMembers.push(friend);

         }//2eP closed
console.log(myFamilyMembers);
}//1eP closed

and here is the output:

[]
[]
[]
[]

In your example, myFriends is an array of objects. (Ln. 1)
Your iteration is for object notation on Ln. 8. the for ... in construct only works on "objects" that have properties. Now, since everything in javascript is an extension of a root "Object" (other than primitives) your code does not fail. All you have done is map the variable "friend" to be a reference to a property of an "array object" that you are then iterating through. You are lucky that you are not getting any errors, as you are trying to reference a property that simply does not exists in the properties of an array, and the JS VM should have considered this a problem.

Here is what you are trying to do...

var myFriends = [
{firstName: "Bertrand", lastName: "Michel"}, 
{firstName: "Michel", lastName: "Bertrand"}, 
{firstName: "Henri", lastName: "Michel"}, {}
]; //this is an array of objects
var myFamily = {lastName: "Michel"}; //this doesnt have to be an object, but whatever.. in fact, you don't even use this?
var myFamilyMembers = []; //this is an empty array
var oObj; //declare outside to save cycles
for (var i=0;i < myFriends.length;i++) { //iterate through the array
    oObj = myFriends[iLup];  //set oObj to be the member of the array by index
    if (oObj.lastName && oObj.lastName == "Michel")  //check the value directly (I think you mean here oObj.lastName == myFamily.lastName)
        {
              myFamilyMembers.push(oObj);
         }
console.log(myFamilyMembers); //Im not sure you want to do this with each pass, but whatever floats your boat...
}

Hope that helps.

Ryan

Although Ryan has provided new code, I would like to suggest you also look at your own code.
You suggested, "something is wrong in the incrementation of the myFamilyMembers array".
Below is a version of your code which will run step by step for you in a browser tab.
You can watch it break, and think about why.
If that is a luxury you feel you can afford.
(Nothing here is meant to offend.)

<html><body>
<script>
alert("simple alert");
    var myFriends = [
    {firstName: "Bertrand", lastName: "Michel"},
    {firstName: "Michel", lastName: "Bertrand"},
    {firstName: "Henri", lastName: "Michel"}, {}
    ];

    var myFamily = {lastName: "Michel"};

    var myFamilyMembers = [];
alert("alert_mF\n myFriends="+myFriends);
var nCount = 0;
    for (var friend in myFriends) {// 1eP
alert("alert_f\n nCount="+nCount+", friend="+friend+", friend.lastName="+friend.lastName);
nCount++;

    if (friend.lastName === myFamily.lastName)
    {//2eP
    myFamilyMembers.push(friend);
    }//2eP closed
    console.log(myFamilyMembers);
    }//1eP closed

var n=0;
    for (var bfriend in myFriends) {
alert("bfriend n="+ n +"\n bfriend[\"lastName\"]="+bfriend["lastName"]);
n++;
    if (bfriend["lastName"]=="Michel") { myFamilyMembers.push(bfriend); }
    }

</script>
</body></html>

@ryantrop
You say:

In your example, myFriends is an array of objects. (Ln. 1) Your iteration is for object notation on Ln. 8.

OK, myFriends is an array and don't be iterated as an object. Good.
But in my precedent code I iterate apparently in correct way,

for (i = 0; i < myFriends.length; i ++) {// 1eP
for (var individu in myFriends[i]) {//2eP
// the iteration will be in the names: "firstName", "lastName"
    if (individu['lastName'] == "Michel")
        {//3eP
              myFamilyMembers.push(myFriends[i]);

and it was unsuccessfull and I get an empty array.
I noticed that instead of

for (var individu in myFriends[i]) {//2eP

that was intendet to represent items of the array that are in reality objects, you created a new variable oObj, (Moreover I do not understand what meant "iLup"). Why is it necessary? Why is it not sufficient to point an item of the array whatsoever?
@actorY
Before thinking I have to go learn about "alert". I don't have it in my toolbox (:

I had a typo on line 10. It should read :

var myFriends = [
{firstName: "Bertrand", lastName: "Michel"}, 
{firstName: "Michel", lastName: "Bertrand"}, 
{firstName: "Henri", lastName: "Michel"}, {}
]; //this is an array of objects
var myFamily = {lastName: "Michel"}; //this doesnt have to be an object, but whatever.. in fact, you don't even use this?
var myFamilyMembers = []; //this is an empty array
var oObj; //declare outside to save cycles
for (var i=0;i < myFriends.length;i++) { //iterate through the array
    oObj = myFriends[i];  //set oObj to be the member of the array by index
    if (oObj.lastName && oObj.lastName == "Michel")  //check the value directly (I think you mean here oObj.lastName == myFamily.lastName)
        {
              myFamilyMembers.push(oObj);
         }
console.log(myFamilyMembers); //Im not sure you want to do this with each pass, but whatever floats your boat...
}

I encourage you to read the comments, and follow along with what the code is actually doing. You are misunderstanding the types of loops presented.

You are confusing an iterative loop (for loop) with an object lookup loop (for-in loop). They are not interchangable in any way, and will give completely different results. Example..

for (var i = 0; i < 10; i++) //this says, as long as i is less than 10, continue doing this
{ 
    console.log(i); //write the value of i to the console
}

for (var c in oObj) //walk through the object oObj, and let the variable c represent the "name" of the parameter
{
  console.log(c); //write the name of the parameter
  console.log(oObj[c]); //write the value of the object with the name represented by the variable c.

}

given the above, you can hopefully see that with what you are trying to do, you don't need a "for-in" loop anywhere. Since object notation works much like associative arrays, you can simply directly access the value you are hoping to find as long as you know it - and since you wrote it, you know it.

Hope that clarifies.

Good luck!!

Ryan

@ryantroop
I read with extreme attention to all that you have written. And clarified my mind.
I illustrate the for in loop and get a surprise, not only the names of the properties are iterated, but also the values. Then I understood what you meant by names of parameters. If I want the names, I need Object.keys(oObj). But the main mistake was to believe that name-value where iterated as diade

var oObj = { "a": 1, "b": 2, "c": 2 }
for (var i = 0; i < 10; i++) //this says, as long as i is less than 10, continue doing this
{
    console.log(i); //write the value of i to the console
}
for (var c in oObj) //walk through the object oObj, and let the variable c represent the "name" of the parameter
{
  console.log(c); //write the name of the parameter
  console.log(oObj[c]); //write the value of the object with the name represented by the variable c.
}
=> output:   
0
1
2
3
4
5
6
7
8
9 -so far we have writen the value of i. 
a
1
b
2
c
2 - and in this second part the names of names
and the name of values 

Sorry, I had forgotten that Object.keys() is an array and needs a classical for loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.