-2
  1. I have a single array of objects.
  2. The objects are different variations of this:
{
    "animals": {
        "name": "elephant",
        "diet": "herbivore",
        "color": "spotted",
        "age": "12",
        "numbers": ["11", "10", "24", "9"]
    }
}

How to iterate through the collection and determine if there are differences between the "numbers" properties or "diet" properties? As in:

In the array, 50 elephant object's "diet" property read "herbivore", but one it reads "omnivore". Break out because it's different and report it.

Or as in:

In the array, 70 elephant object's "numbers" property are the same (even if some are out of order). Report they are the same.

What did I try?:

  1. Stringifying the objects and comparing them, but that won't work because I'm not trying to compare whole objects (just a few individual properties).
  2. I can make this work with a for loop, but it seems stupid overkill and not very efficient for a collection that potentially has hundreds of objects in it.

This is pseudo code, don't freak out:

var isValid = true;

//go through each element in the array
for(let i = 0, i < array.length, i++) {
    //check if you have exceeded length of the array
    if(i + 1 <= array.length) {

        //sort the numbers
        var sortedLowerNumbers = sortFunction(array[i].numbers);
        var sortedhigherNumbers = sortFunction(array[i+1].numbers);
        //compare equality 
       isValid = json.stringify(sortedLowerNumbers) === json.stringify(sortedhigherNumbers);
        
       if(!isValid) {
         break
       }
    }
}

My research

None of the questions seem applicable. They're seem to be either comparing multiple arrays rather than one or comparing entire JSON objects for equality rather than comparing individual properties.

Is there a smarter way to do this?

0

1 Answer 1

1

Although a for loop is the best and most simple way to do it. if you are looking for brevity you can use .every():

let arr = [{
        name: "elephant",
        diet: "herbivore",
        color: "spotted",
        age: "12",
       numbers: ["11", "10", "24","9"]
},{             name: "elephant",
        diet: "herbivore",
        color: "spotted",
        age: "12",
       numbers: ["11", "10", "24","9"]
},{
                name: "elephant",
        diet: "herbivore",
        color: "spotted",
        age: "12",
       numbers: ["11", "10", "24","9" , "23"]
}];
let arrSort = arr[0].numbers.sort();
let JSONarrSort = JSON.stringify(arrSort);
console.log(arr.every(x => JSON.stringify(x.numbers.sort()) === JSONarrSort));
console.log(arr.every(x => x.diet === arr[0].diet));

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

1 Comment

From the OP: In the array, 50 elephant object's "diet" property read "herbivore", but one it reads "omnivore". Break out because it's different and report it.. So, should we consider the second console be changed to something like: arr.some(x => x.diet !== arr[0].diet?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.