0

I am trying to search an Array by name and ingredients but I am really failing to do it. This is my first time using Search and React Native in general as well as my basic knowledge in javascript. Can someone help me out . I am getting an empty array.

This is an example of the Data when it is console.log:

Array [
    Object {
      "id": "VpcupAWap8RRIbLSt0ZuleCh2C22",
      "ingridients": Array [
        Object {
          "id": 1598773734254,
          "val": '3 ½ cups tomato sauce (800 g)'
        },
        Object {
          "id": 1598773734154,
          "val": '1 large onion, finely chopped'
        },
        Object {
          "id": 1598773734154,
          "val": '8 cloves garlic, minced'
        },
      ],
      "name": "Chicken Sandwich",
    },
]

This is my code :

import React, { useState } from "react";
import { SearchBar } from "react-native-elements";

const SearchScreen = (props) => {
    const [searchDetails, setSearchDetails] = useState();
    const [data, setData] = useState([]);
    const recipes = props.navigation.getParam('recipes')

    console.log(recipes)
    const searchFilterFunction=(text)=>{
        setSearchDetails(text)
        const newData = recipes.filter(
            item => { item.name === text}
        )
        setData(newData)
        }
        console.log(data)
    return (
        <View style={styles.container}>
            <SearchBar
                placeholder="Type Here..."
                onChangeText={(text) => searchFilterFunction(text)}
                value={searchDetails}
            />
        </View>
    );
};

export default SearchScreen;
4
  • can you create a reproducible issue here? Commented Aug 30, 2020 at 13:35
  • It renders an empty list for some reason Commented Aug 30, 2020 at 13:38
  • const newData = recipes.filter(item => item.name === text) try doing this? Commented Aug 30, 2020 at 13:41
  • But it won't go into the new array or even search the ingridients please show me I don't know what I'm doing at all Commented Aug 30, 2020 at 13:50

2 Answers 2

1

here is the function to search your data by name and ingredients

const searchFilterFunction = (searchTerm) => {
  if (searchTerm) {
    setSearchDetails(searchTerm);
    const newData = foods.filter(({ name, ingredients }) => {
      if (RegExp(searchTerm, "gim").exec(name))
        return RegExp(searchTerm, "gim").exec(name);
      else
        return ingredients.filter(({ val }) =>
          RegExp(searchTerm, "gim").exec(val)
        ).length;
    });

    setData(newData);
  } else {
    setData([]);
  }
};

This function will first search for the foods by name if there no match then it searches on ingredients. I have tested this and this function works

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

1 Comment

Wow thank you so much for your answer this is exactly what I was trying to do. How would I edit this code to only show the searched results and not all the foods before anything is even searched. Again thank you for you answer
0

Here's a simplified version of your app.

You could check the example here

import React, { useState } from "react";
import { View, StyleSheet, Text } from 'react-native'
import { SearchBar } from "react-native-elements";

const styles = StyleSheet.create({});

const recipes = ['chicken', 'potato', 'burger'];

const SearchScreen = (props) => {
    const [searchDetails, setSearchDetails] = useState('');
    const [data, setData] = useState(recipes);

    const searchFilterFunction=(text)=>{
        const newData = recipes.filter(item => item.includes(text.toLowerCase()))
        setData(newData)
    }


    return (
        <View style={styles.container}>
            <SearchBar
                placeholder="Type Here..."
                onChangeText={(text) => searchFilterFunction(text)}
                value={text => setSearchDetails(text)}
            />
            <View>
            {data.map(text => <Text>{text}</Text>)}
            </View>
        </View>
    );
};

export default SearchScreen;

3 Comments

Hi thank you for your answer are you able to change the recipe to an array of objects because that’s what I am finding difficult
yeah you can just insert your array here and change the search function
const newData = recipes.filter(item => item.text.toLowerCase().includes(text.toLowerCase())) Something like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.