skip to Main Content

I have an array

0: key: "001" value: Array(1) 0: Id: "2345" Name: "Test"
1: key: "112" value: Array(2) 0: Id: "1234" Name: "UHV" 1: Id: "3424" Name: "ABC"
2: key: "222" value: Array(3) 0: Id: "2312" Name: "ABD" 1: Id: "1321" Name: "RFV" 2: Id: "4567" Name: "ERF"

when i try to Search with ‘AB’ my logic should search my array object and return . please help me for the logic

1: key: "112" value: Array(1) 0: Id: "3424" Name: "ABC"
2: key: "222" value: Array(1) 0: Id: "2312" Name: "ABD"

3

Answers


  1. First you given JSON Array is wrong. I corrected it based on assumption:

    var arr =[
     {"key": "001", "value":[{ "Id": "2345", "Name": "Test"}]},
     {"key": "112", "value": [{"Id": "1234" ,"Name": "UHV"} ,{ "Id": "3424" ,"Name": "ABC"}]},
     {"key": "222", "value":  [{"Id": "2312" ,"Name": "ABD"} ,{ "Id": "1321", "Name": "RFV"} ,{"Id": "4567" ,"Name": "ERF"}]}
     ];
    

    Secondly, you can achieve this via using Array some()

    Refer to the below code for reference:

    var arr =[
     {"key": "001", "value":[{ "Id": "2345", "Name": "Test"}]},
     {"key": "112", "value": [{"Id": "1234" ,"Name": "UHV"} ,{ "Id": "3424" ,"Name": "ABC"}]},
     {"key": "222", "value":  [{"Id": "2312" ,"Name": "ABD"} ,{ "Id": "1321", "Name": "RFV"} ,{"Id": "4567" ,"Name": "ERF"}]}
     ];
     
     function filterValues(arr) {
      const filteredValues = arr.filter(item => {
        if (Array.isArray(item.value)) {
          return item.value.some(innerItem => innerItem.Name === "ABC" || innerItem.Name === "ABD");
        }
        return false;
      });
    
      return filteredValues;
    }
    
    console.log(filterValues(arr))
    Login or Signup to reply.
  2. You can use Array::filter() to find AB Name in value with String::includes() and compose the root filtered array with Array::reduce(). The filtered value array items are composed with spread and destructuring syntax:

    const arr =[
     {"key": "001", "value":[{ "Id": "2345", "Name": "Test"}]},
     {"key": "112", "value": [{"Id": "1234" ,"Name": "UHV"} ,{ "Id": "3424" ,"Name": "ABC"}]},
     {"key": "222", "value":  [{"Id": "2312" ,"Name": "ABD"} ,{ "Id": "1321", "Name": "RFV"} ,{"Id": "4567" ,"Name": "ERF"}]}
     ];
     
    const filtered = arr.reduce((arr, {value, ...item}) => {
       const found = value.filter(item => item.Name.includes('AB'));
       found.length && arr.push({...item, value: found});
       return arr;
    }, []);
    
    console.log(JSON.stringify(filtered));
    Login or Signup to reply.
  3. here are three simple examples of how you can filter the array based on your criteria

    1. (Using filter and some):
        const arr = [
      {"key": "001", "value": [{"Id": "2345", "Name": "Test"}]},
      {"key": "112", "value": [{"Id": "1234", "Name": "UHV"}, {"Id": "3424", "Name": "ABC"}]},
      {"key": "222", "value": [{"Id": "2312", "Name": "ABD"}, {"Id": "1321", "Name": "RFV"}, {"Id": "4567", "Name": "ERF"}]}
    ];
    
    function filterValues(arr) {
      const filteredValues = arr.filter(item => {
        if (Array.isArray(item.value)) {
          return item.value.some(innerItem => innerItem.Name.includes("AB"));
        }
        return false;
      });
    
      return filteredValues;
    }
    
    console.log(filterValues(arr));
    
    1. (Using reduce):
        const arr = [
      {"key": "001", "value": [{"Id": "2345", "Name": "Test"}]},
      {"key": "112", "value": [{"Id": "1234", "Name": "UHV"}, {"Id": "3424", "Name": "ABC"}]},
      {"key": "222", "value": [{"Id": "2312", "Name": "ABD"}, {"Id": "1321", "Name": "RFV"}, {"Id": "4567", "Name": "ERF"}]}
    ];
    
    const filtered = arr.reduce((result, { value, ...item }) => {
      const found = value.filter(innerItem => innerItem.Name.includes('AB'));
      if (found.length) {
        result.push({ ...item, value: found });
      }
      return result;
    }, []);
    
    console.log(filtered);
    
    1. (Using ES6 syntax and arrow functions):
    const arr = [
      {"key": "001", "value": [{"Id": "2345", "Name": "Test"}]},
      {"key": "112", "value": [{"Id": "1234", "Name": "UHV"}, {"Id": "3424", "Name": "ABC"}]},
      {"key": "222", "value": [{"Id": "2312", "Name": "ABD"}, {"Id": "1321", "Name": "RFV"}, {"Id": "4567", "Name": "ERF"}]}
    ];
    
    const filtered = arr.filter(({ value }) => value.some(({ Name }) => Name.includes('AB')));
    
    console.log(filtered);
    

    All three examples will produce the same output:

    [  {"key": "112", "value": [{"Id": "3424", "Name": "ABC"}]},
      {"key": "222", "value": [{"Id": "2312", "Name": "ABD"}]}
    ]
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search