Back

Technologies:

javascriptjavascript
avatar
Tolerim
7 hours ago

How do I compare two objects that have equal values?

My objective is to create a new array that stores the id and name of fruits that are not present in the food array by comparing the two arrays.

For instance, in the given arrays, the food array contains Orange, but Apple and Cherry are not present in it. Therefore, the new array should save the id and name of these two fruits that do not exist in the food array. Here is the code that I have tried:

            
                const fruits = [{id: '1', name: 'Apple'},
                {id: '2', name: 'Orange'},
                {id: '3', name: 'Cherry'}];
                
                const food=[{id: '1', creationdate: '2023-05-13 09:46:25', createdby: '1'},
                {id: '1', food_name: 'Orange'},
                {id: '2', food_name: 'Bread'},
                {id: '3', food_name: 'Chees'},
                {id: '4', food_name: 'Milk'},
                {id: '5', food_name: 'Salt'}
                ]
                
                var res = {};
                var dep_data = [];
                for (var j = 0; j < fruits.length; j++) {
                  for (var d = 0; d < food.length; d++) {
                    if (parseInt(fruits[j].id) != parseInt(food[d])) {
                      res["id"] = fruits[j].id;
                      res["name"] = fruits[j].name;
                      dep_data.push(res);
                    }
                  }
                }
                console.log(dep_data)
            
        
Answers(1)
avatar
Tolerim
7 hours ago
Verified Answer
To compare the two arrays and get the id and name of fruits that do not exist in the food array, you can use the filter() and find() array methods. The filter() method is used to filter out the elements that match a certain condition, while the find() method is used to return the first element that matches a certain condition within an array. Here's an example code:
const fruits = [{id: '1', name: 'Apple'},
{id: '2', name: 'Orange'},
{id: '3', name: 'Cherry'}];

const food=[{id: '1', creation_date: '2023-05-13 09:46:25', created_by: '1'},
{id: '1', food_name: 'Orange'},
{id: '2', food_name: 'Bread'},
{id: '3', food_name: 'Chees'},
{id: '4', food_name: 'Milk'},
{id: '5', food_name: 'Salt'}
];

const result = fruits.filter(fruit => !food.find(f => f.food_name === fruit.name))
    .map(fruit => ({id: fruit.id, name: fruit.name}));

console.log(result);
In this example, we first use the filter() method to filter out the fruits that have a name that exists in the food array. We use the find() method inside the filter() method to check whether each fruit's name exists in the food array. If it does not exist, the filter() method includes the fruit in the result array. Finally, we use the map() method to transform the result array to an array that only includes the id and name properties of the fruits. The map() method creates a new array by applying a mapping function on each element of the input array. The output of the example code will be an array that includes the id and name of the fruits that do not exist in the food array:
[
    {id: '1', name: 'Apple'},
    {id: '3', name: 'Cherry'}
]
;