1

I would appreciate any help with this.

This has been edited to more clearly describe how the array should be evaluated.

I am trying to figure out how I can compare all elements of a multidimensional array to determine if the elements comprise a true set.

Criteria

  1. I won't know how big the array is going to be ahead of time (could be 2, could be 1000).
  2. Each array element has two characteristics (color and type) that need to be compared.
  3. If any charactistic value is shared by any two elements then all elements must share that characteristic value.
  4. The values for 'color' & 'type' can be compared as straight text, they will match or not, no variation will be possible. I.e. Green != Greene.
  5. The function only needs to return true or false based on whether is it a true set or not.
  6. Any assistance can be given in JavaScript, PHP, or C. I can extrapolate.

Array Example

var obj = {  
    'Element001' : {  
        'ID' : 'value',  
        'Color' : 'value',  
        'Type' : 'value'  
    },  
    'Element002' : {  
         'ID' : 'value',  
         'Color' : 'value',  
         'Type' : 'value'  
    }  
    ...  
};  

Examples of how different arrays would evaluate.

The following array should evaluate to "TRUE" because all elements are red and no elements share a type.

var obj = {  
    'Element001' : {  'ID' : '1', 'Color' : 'red', 'Type' : 'b'  },  
    'Element002' : {  'ID' : '2', 'Color' : 'red',  'Type' : 'a'  }  
    'Element003' : {  'ID' : '21', 'Color' : 'red', 'Type' : 'c'  }  
};  

The following array should evaluate to "TRUE" because all elements are of type "b" and no elements share a color.

var obj = {  
    'Element001' : {  'ID' : '1', 'Color' : 'red', 'Type' : 'b'  },  
    'Element002' : {  'ID' : '2', 'Color' : 'blue', 'Type' : 'b'  }  
    'Element003' : {  'ID' : '21', 'Color' : 'green', 'Type' : 'b'  }  
};  

The following array should evaluate to "TRUE" because all elements are "blue" and all elements are of type "c".

var obj = {  
    'Element001' : {  'ID' : '1', 'Color' : 'blue',  'Type' : 'c'  },  
    'Element002' : {  'ID' : '2', 'Color' : 'blue',  'Type' : 'c'  }  
    'Element003' : {  'ID' : '21', 'Color' : 'blue',  'Type' : 'c'  }  
};  

The following array should evaluate to "FALSE" because two elements share a color "red". Per rule: If any two elements share a characteristic they must all share it. Thus all elements would have to be red. Also in this example no elements share a type. But even if they were all of type "a" it would still evaluate to "FALSE", because of the color attribute.

var obj = {  
    'Element001' : {  'ID' : '1', 'Color' : 'red', 'Type' : 'b'  },  
    'Element002' : {  'ID' : '2', 'Color' : 'red', 'Type' : 'a'  },  
    'Element003' : {  'ID' : '21', 'Color' : 'blue', 'Type' : 'c'  }  
};

Also another that would evaluate to "FALSE" is a set where no characteristic is shared.
Such as one like this...

var obj = {  
    'Element001' : {  'ID' : '1', 'Color' : 'red', 'Type' : 'b'  },  
    'Element002' : {  'ID' : '2', 'Color' : 'green', 'Type' : 'a'  },  
    'Element003' : {  'ID' : '21', 'Color' : 'blue', 'Type' : 'c'  }  
};
13
  • 1
    @CIsForCookies Because he only wants to compare Color and Type. Not the entire object. Commented Jul 20, 2017 at 5:19
  • 1
    @ClsForCookies Sometimes, that can produce a false negative. Commented Jul 20, 2017 at 5:21
  • 1
    The format given isn't an actual array. It is a nested object where the first level of properties uses a name that is more or less abused to suggest sequence. Why not use an actual array of objects? Commented Jul 20, 2017 at 5:23
  • 1
    @ClsForCookies When this has bit me, the string representations are pretty long. Don't think I can give you a reliable case. Commented Jul 20, 2017 at 5:25
  • 1
    @Paul I'm not sure about long string representations but JSON.stringify() does rely on the order of properties being the same. Technically 2 objects are still the same even if the order is different. That's where it produces a false negative. Commented Jul 20, 2017 at 5:32

2 Answers 2

1

Follow Following Example

var obj = {  
    'Element001' : {  
        'ID' : '1',  
        'Color' : 'Red',  
        'Type' : 'X'  
    },  
    'Element002' : {  
         'ID' : '2',  
         'Color' : 'Green',  
         'Type' : 'X'  
    }  
};

for(var x in obj){
    if(obj[x].Color=="Green")
    {
       console.log("ID : " +obj[x].ID+" has color Green");
    }
}

If you don't want like that then you can use this code

var obj = {  
    'Element001' : {  'ID' : '1', 'Color' : 'red', 'Type' : 'b'  },  
    'Element002' : {  'ID' : '2', 'Color' : 'red',  'Type' : 'a'  },
    'Element003' : {  'ID' : '21', 'Color' : 'red', 'Type' : 'c'  }  
};

var Keys=Object.keys(obj);
var _color=obj[Keys[0]].Color;
var _type=obj[Keys[0]].Type;
var _isSameColor=true;
var _isSameType=true;
for(var x=1;x<Keys.length;x++){
    _isSameColor=_isSameColor&_color==obj[Keys[x]].Color;
    _isSameType=_isSameType&_type==obj[Keys[x]].Type;
}
console.log(_isSameColor);
console.log(_isSameType);
if(_isSameColor||_isSameType){
   alert("True");
}
else{
   alert("False");
}

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

6 Comments

Please explain more so I can give answer like that I am not getting what you want
I am not testing for a specific value. I will not know what the values might be. I have added examples. Thank you for your time.
try second Example It will be helpful like you want
I think you want like that. Please accept answer if you got solution
Sorry about taking a bit getting back to this. The second one is perfect. I can totally rewrite this in C and wrap it in a function. Thank you for your help!
|
0

You should use square bracket symbol : []

var obj = [  
        'Element_1' : {  
            'ID' : '1',  
            'Color' : 'Red',  
            'Type' : 'X'  
        },  
        'Element_2' : {  
             'ID' : '2',  
             'Color' : 'Green',  
             'Type' : 'X'  
        }  
    ];

   for(var i = 0 ; i++ ;  i < obj.length){
    if(obj[i].Color=="Green") {
     console.log("ID : " +obj[i].ID+" has color Green");
    }
   }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.