How to compare array values with object properties in this example?

array of objects javascript
javascript update object property in array
javascript update object in array es6
javascript object with array property
how to access array of objects in javascript
javascript find object in array by property value
nodejs array of objects
add property to array of objects typescript

This is my array:

const ids = [
  "id1",
  "id2",
  "id3"
]

Object that is extracted via cookies

const data = [
    {
      id: "id3" // dynamically populated and can vary, we need this property
      prop2: "prop2", // not needed
      prop3: "prop3" // not needed
    }
]

How can I compare first array values - ids with data.id, and if true, show some logic (for example - console.log(true)? I need it in if statement.

Please note that data.id. can be anything, from id1 to id3. I need to find a way to dynamically compare these values. In "real world" it id anything from 1 do 50000, but this is just a mock-up for the example.

Also I would appreciate Lodash example.

For each item in data, you check if the id is in the array of ids, and if yes, do something

const ids = [
    "id1",
    "id2",
    "id3"
]

const data = [
  {
      id: "id3", // dynamically populated and can vary, we need this property
      prop2: "prop2", // not needed
      prop3: "prop3" // not needed
   }
]

// if we have the id in ids array, do something
if (data.some(d => ids.indexOf(d.id) > -1)) {
  // do something
}

Using lodash:

const ids = [
    "id1",
    "id2",
    "id3"
]

const data = [
  {
      id: "id3", // dynamically populated and can vary, we need this property
      prop2: "prop2", // not needed
      prop3: "prop3" // not needed
   }
]

// if we have the id in ids array, do something
if (_.some(data, d => ids.indexOf(d.id) > -1)) {
  // do something
}

method to compare two arrays - Confluence Mobile, How do I compare two arrays of objects in Lodash? You can see below that the Compare-Object cmdlet allows you to compare both arrays at once. If the SideIndicator property is =>, this means the InputObject property returned is in the DifferenceObject value and not in the ReferenceObject value and vice versa for the <= SideIndicator. By default,

You can iterate all the elements in Array which are objects and using Object.keys, iterate all the keys in that object which could be compared with initial array of values.

const ids = [
  "id1",
  "id2",
  "id3"
];

const data = [{
  id: "id3", // dynamically populated and can vary, we need this property
  prop2: "prop2", // not needed
  prop3: "prop3" // not needed
}];

const foundElems = [];
data.forEach((el) => {
  Object.keys(el).forEach(elem => {
    if (ids.indexOf(el[elem]) > -1) {
      var Obj = {};
      Obj[elem] = el[elem];
      foundElems.push(Obj);
    }
  });
});
console.log(foundElems);

How to compare array values with object properties in this example?, You can iterate all the elements in Array which are objects and using Object.keys , iterate all the keys in that object which could be compared  The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. The result of the comparison indicates whether a property value appeared only in the reference object (<=) or only in the difference object (=>).

I think this is what you are looking for:

if (ids.indexOf(data.id) !== -1){
  console.log('true')
}

Useful Javascript Array and Object Methods, The returned value can be of any type (i.e. object, array, string, integer). Example Example. Check if all ratings are equal to or greater than 3 stars. Add a new object property and value without mutating the original object. A list. Objects, as generic blobs of values, can be used to build all sorts of data structures. A common data structure is the list (not to be confused with array). A list is a nested set of objects, with the first object holding a reference to the second, the second to the third, and so on.

Just run the next code:

<html>
<body>
    <button onclick="compareIds()">Click me</button>
    <script>
        const ids = [
              "id1",
              "id2",
              "id3"
            ]

        const data = [
                {
                  id: "id3", // dynamically populated and can vary, we need this property
                  prop2: "prop2", // not needed
                  prop3: "prop3" // not needed
                }
            ]

        function compareIds(){
                var dataObject = findDataInObject();
                for(var i = 0; i < ids.length; i++){
                        if (ids[i] == dataObject){
                            console.log("true");
                        }

                        else{
                            if(i == ids.length -1){
                                console.log("false");
                            }
                    }
                }
        }

        function findDataInObject(){
                    for(key in data) {
                        if(data.hasOwnProperty(key)) {
                            var ArrayOfData = data[key];
                            var IdOfData = ArrayOfData.id;
                            return IdOfData;
                        }
                    }
                }


    </script>
</body>

Data Structures: Objects and Arrays :: Eloquent JavaScript, The example code will often build on functions and bindings that were introduced Both string and array values contain, in addition to the length property, a number of When you compare objects with JavaScript's == operator, it compares by  How to compare array contents? A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. Actually, there is a list of equals() methods in Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is base of all classes in Java).

Check if two arrays or objects are equal with JavaScript, We'll accept two arguments: the value is one array or object, and other false; // Compare properties if (type === '[object Array]') { for (var i = 0;  Return value. An array containing the given object's own enumerable property values. Description. Object.values() returns an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by looping over the property values of the object manually. Examples Using Object.values

Object Equality in JavaScript, You might suppose that if two objects have the same properties and all of their properties Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and plain Here is an example of how that works. myNumbers is now an array with two arrays as its elements. To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array. This example accesses the third element (2) in the second array (1) of myNumbers:

Testing Arrays and Objects with Chai.js - Building Ibotta, The equality being expressed in the example above is actually a core Similar to arrays, comparing two objects will use strict or deep equality. An third way to compare two arrays of primitive values is to use the flagging property deep. Briefly, Objects Comparer is an object-to-object comparer, which allows to compare objects recursively member by member and define custom comparison rules for certain properties, fields or types. Objects Comparer could be used in .NET Core projects (.NET Standard 1.6, 2.0 or above) and in .NET projects (version 4.5 or above).

Comments
  • Your mean is ids[0] == data.id, aren't you ?
  • Yes in this example, but data.id can be anything - from id1 to id3 (in real world it can be up to 50000, this is just for this question example). Also, please don't downvote, or at least write why.
  • I think you have been downvoted because by the sites rules, you need to show in your question some code where you tried to sleve your problem, which you do not have.
  • Your question is unclear, you want check data.id match anything of ids ?
  • That is, Lazar, because I don't know how to compare in if else..
  • You can user lodash as well. logic remains same. _.forEach is what you need.
  • Ok, but I need to do it in if else statement.
  • Read again please.