0

I have an array off Object inside a variable named data. The array looks like this:

const data = [
      {
        "id": "0.804802585702977",
        "value": "Bar",
      },
      {
        "id": "0.9359341974615858",
        "value": "Mar",
      },
      {
        "id": "0.4182922963461958",
        "value": "Naba",
      },
      {
        "id": "0.6132336648416628",
        "value": "Socerr",
      },
      {
        "id": "0.060587558948085984",
        "value": "Mall",
      },
    ]

I want to create a search bar to search all the value inside that array and if the search text is equal to the value inside that array the user will be able to see that value? Please help?

1 Answer 1

1

You need to use filter function to search/Sort data from Array. Please check following it may solve you problem.

import React, { useState } from "react";
import { Text, TextInput, View, StyleSheet } from "react-native";
import Constants from "expo-constants";

// You can import from local files
import AssetExample from "./components/AssetExample";

// or any pure javascript modules available in npm
import { Card } from "react-native-paper";

const data = [
  {
    id: "0.804802585702977",
    value: "Bar",
  },
  {
    id: "0.9359341974615858",
    value: "Mar",
  },
  {
    id: "0.4182922963461958",
    value: "Naba",
  },
  {
    id: "0.6132336648416628",
    value: "Socerr",
  },
  {
    id: "0.060587558948085984",
    value: "Mall",
  },
];
export default function App() {
  let [filteredData, setFilteredData] = useState(data);

  function _searchFilterFunction(searchText, data) {
    let newData = [];
    if (searchText) {
      newData = data.filter(function(item) {
        const itemData = item.value.toUpperCase();
        const textData = searchText.toUpperCase();
        return itemData.includes(textData);
      });
      setFilteredData([...newData]);
    } else {
      setFilteredData([...data]);
    }
  }

  return (
    <View style={styles.container}>
      <Text style={styles.paragraph}>Search Here.</Text>
      <TextInput
        style={styles.input}
        underlineColorAndroid="transparent"
        placeholder="Search"
        placeholderTextColor="#9a73ef"
        autoCapitalize="none"
        onChangeText={(value) => {
          _searchFilterFunction(value, data);
        }}
      />
      {filteredData.map((item, index) => {
        return <Text style={styles.paragraph}>{item.value}</Text>;
      })}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,

    paddingTop: Constants.statusBarHeight,
    backgroundColor: "#ecf0f1",
    padding: 8,
  },
  input: {
    margin: 15,
    height: 40,
    paddingLeft: 10,
    borderRadius: 2,
    borderColor: "#7a42f4",
    borderWidth: 1,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: "bold",
    textAlign: "center",
  },
});
Sign up to request clarification or add additional context in comments.

Comments

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.