skip to Main Content
const fruits = [{id: '1', name: 'Apple'},
{id: '2', name: 'Orange'},
{id: '3', name: 'Cherry'}];

const food=[{id: '1', food_name: 'Orange', deleted:"0"},
{id: '2', food_name: 'Bread' ,deleted:"0"},
{id: '3', food_name: 'Cheese', deleted:"0"},
{id: '4', food_name: 'Apple', deleted:"1"},
{id: '5', food_name: 'Salt',deleted:"0"}
]
//Code that I tried:
dep_data=[];
var foodSet = new Set(food.map(item => item.food_name));
for(var j=0; j < fruits.length; j++){
    if(!foodSet.has(fruits[j].name) && fruits[j].deleted!=1){
        dep_data.push({id: fruits[j].id, name: fruits[j].name});
    }
}
console.log(dep_data)

I want to compare between two arrays to get the id and name of the fruits that not exists in the food and deleted not equal to 1 then save the results in new array.

For example here we have the Orange exist in the food array, the results should store the id and name of fruits that doesn’t exist in food and deleted !=1. (Apple, cherry).

2

Answers


  1. Your code has syntax errors, here is the updated one:

    const fruits = [
      { id: '1', name: 'Apple' },
      { id: '2', name: 'Orange' },
      { id: '3', name: 'Cherry' }
    ];
    
    const food = [
      { id: '1', food_name: 'Orange', deleted: "0" },
      { id: '2', food_name: 'Bread', deleted: "0" },
      { id: '3', food_name: 'Cheese', deleted: "0" },
      { id: '4', food_name: 'Apple', deleted: "1" },
      { id: '5', food_name: 'Salt', deleted: "0" }
    ];
    
    var dep_data = [];
    var foodSet = new Set(food.map(item => item.food_name));
    
    for (var j = 0; j < fruits.length; j++) {
      if (!foodSet.has(fruits[j].name) && fruits[j].deleted !== "1") {
        dep_data.push({ id: fruits[j].id, name: fruits[j].name });
      }
    }
    
    console.log(dep_data);
    
    

    The result will return an array object containing cherry

    Login or Signup to reply.
  2. The code you provided is almost correct. There’s just one small mistake in the comparison of the deleted property. Instead of comparing it to the string "1", you should compare it to the number 1. Here’s the corrected code:

    const fruits = [
      { id: '1', name: 'Apple' },
      { id: '2', name: 'Orange' },
      { id: '3', name: 'Cherry' }
    ];
    
    const food = [
      { id: '1', food_name: 'Orange', deleted: "0" },
      { id: '2', food_name: 'Bread', deleted: "0" },
      { id: '3', food_name: 'Cheese', deleted: "0" },
      { id: '4', food_name: 'Apple', deleted: "1" },
      { id: '5', food_name: 'Salt', deleted: "0" }
    ];
    
    const dep_data = [];
    var foodSet = new Set(food.map(item => item.food_name));
    for (var j = 0; j < fruits.length; j++) {
      if (!foodSet.has(fruits[j].name) && fruits[j].deleted != '1') {
        dep_data.push({ id: fruits[j].id, name: fruits[j].name });
      }
    }
    console.log(dep_data);
    

    The output will be:

    [ { id: '1', name: 'Apple' }, { id: '3', name: 'Cherry' } ]
    

    This means that the fruits "Apple" and "Cherry" are not present in the food array and have deleted not equal to 1, so they are stored in the dep_data array.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search