skip to Main Content

Here is my array, i try to compare multiple ids with id which is in nestead array in react native
here is function to compare with multiple id with selected ids :
const [addedItems,setItems] = useState(props.addItemsToCart)
const [selectedItems, setSelectedItems] = useState([]);
//my selectd items is : ex [0,4] useEffect(() => {
const addedInCart = ResturentData.filter(item => item.foodItem.some(item => addedItems.includes(item.id)))
setSelectedItems(addedInCart)

    },[addedItems])  
Here is my array,
export const ResturentData = [
  {
    title: 'foodResturants.recommended',
    foodItem: [
      {
        id: 0,
        image: resturantProduct7,
        title: 'foodResturants.vegCheese',
        rate: '4.5',
        price: 25,
        items: 1,
        clickedItem: false,
        quantity: 1,
      },
     {
        id: 1,
        image: resturantProduct4,
        title: 'foodResturants.barbarescaPasta',
        rate: '4.5',
        price: 25,
        items: 1,
        clickedItem: false,
        quantity: 1,
      },
      
    ],
  },
  {
    title: 'foodResturants.quickBites',
    foodItem: [
      {
        id: 2,
        image: resturantProduct6,
        title: 'foodResturants.fries',
        rate: '4.5',
        price: 25,
        items: 1,
        clickedItem: false,
        quantity: 1,
      }
    ],
  },
];   

i try to compare multiple ids with id which is in nestead array  in react native
here is function to compare with multiple id with selected ids :

But i got result is different items how to fix this thanks in advance!

2

Answers


  1. you can use JavaScript's array methods like map, some, or find
    
    const data = [
      { id: 1, name: 'Item 1' },
      { id: 2, name: 'Item 2' },
      { id: 3, name: 'Item 3' },
      // ... more objects
    ];
    
    const targetId = 2; 
    
    const hasTargetId = data.some(item => item.id === targetId);
    
    if (hasTargetId) {
      console.log(`An object with ID ${targetId} exists in the array.`);
    } else {
      console.log(`No object with ID ${targetId} found.`);
    }
    
    Login or Signup to reply.
  2. You can achieve it in multiple ways. I will show you 2 common and standard methods for this:

    1. Using descriptive logical method (This is more understandable but a little complex way)

    const givenId = 2; // Replace this with the ID you want to compare
    
    // Function to compare IDs
    const compareIds = (data, targetId) => {
      for (const restaurant of data) {
        for (const foodItem of restaurant.foodItem) {
          if (foodItem.id === targetId) {
            return true; // Found a match
          }
        }
      }
      return false; // No match found
    };
    
    // Usage
    const isIdPresent = compareIds(RestaurantData, givenId);
    
    if (isIdPresent) {
      console.log(`ID ${givenId} is present in the RestaurantData array.`);
    } else {
      console.log(`ID ${givenId} is not present in the RestaurantData array.`);
    }
    

    2. Using array prototype provided by javascript library (Shortcut and simplest way)

    const givenId = 2; // Replace this with the ID you want to compare
    
    // Function to compare IDs using some() method
    const isIdPresent = RestaurantData.some(restaurant =>
      restaurant.foodItem.some(foodItem => foodItem.id === givenId)
    );
    
    if (isIdPresent) {
      console.log(`ID ${givenId} is present in the RestaurantData array.`);
    } else {
      console.log(`ID ${givenId} is not present in the RestaurantData array.`);
    }
    

    In the 2nd method, we use the some() method twice: first to iterate through the RestaurantData array, and then to iterate through the foodItem arrays within each restaurant. If any of the foodItem arrays contains an object with a matching ID, the some() method will return true, indicating that the ID is present.

    This 2nd approach is more concise and aligns with JavaScript’s functional programming paradigm. It also makes the code easier to read and understand.

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