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
- I won't know how big the array is going to be ahead of time (could be 2, could be 1000).
- Each array element has two characteristics (color and type) that need to be compared.
- If any charactistic value is shared by any two elements then all elements must share that characteristic value.
- 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.
- The function only needs to return true or false based on whether is it a true set or not.
- 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' }
};
ColorandType. Not the entire object.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.