Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
How to filter a multi dimensional json file to match the input value

I am triying filter a json file to match the input value. I write the code below. The json file is multi dimensional.

var object = [{"key1" : "Test value 1",
    "key3" : [{
    "key4" : "Test value 3",
    "key5" : "Test value 4"
    },
    {
    "key4" : "Test value 5",
    "key5" : "Test value 6"
    }]
    },
    {
    "key1" : "Test value 11",
    "key3" : [{
    "key4" : "Test value 13",
    "key5" : "Test value 14"
    },
    {
    "key4" : "Test value 15",
    "key5" : "Test value 16"
    }]
    }];

   const search = document.getElementById("search");
    const matchList = document.getElementById("match-list");
    
    searchStates = searchText => {
        
        const states = object;
        
        let matches = states.filter(state => {
            const regex = new RegExp(`^${searchText}`, 'gi');
            
            return state.key3.key4.match(regex);
        });
        console.log(matches);
    };
    
    search.addEventListener("input", () => searchStates(search.value));
<input type="text" id="search" class="form-control form-control-lg" placeholder="type here">
<div id="match-list"></div>

I need to match input with key 4 and need to remove duplicate values. How to do it? I tried with

states.key3.filter(...state.key4 but it give errors

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can get the filtered result from the key4, so you can use some and startsWith

let matches = states.filter( state => state.key3.some( o => o.key4.toLowerCase().startsWith( searchText.toLowerCase() ) ) );

var object = [
    {
        "key1": "Test value 1",
        "key3": [{
            "key4": "Test value 3",
            "key5": "Test value 4"
        },
        {
            "key4": "Test value 5",
            "key5": "Test value 6"
        }]
    },
    {
        "key1": "Test value 11",
        "key3": [{
            "key4": "Test value 13",
            "key5": "Test value 14"
        },
        {
            "key4": "Test value 15",
            "key5": "Test value 16"
        }]
    }];

const search = document.getElementById( "search" );
const matchList = document.getElementById( "match-list" );

searchStates = searchText => {
    const states = object;
    let matches = states.filter( state => state.key3.some( o => o.key4.toLowerCase().startsWith( searchText.toLowerCase() ) ) );
    console.log( matches );
};

search.addEventListener( "input", ( e ) => {
    searchStates( e.target.value )
} );
<input type="text" id="search" class="form-control form-control-lg" placeholder="type here">
<div id="match-list"></div>

about 4 years ago · Juan Pablo Isaza Report

0

This will show the object that has a key4 value exactly equal to the search input:

var object = [
  { key1: 'Test value 1', key3: [ 
    { key4: 'Test value 3', key5: 'Test value 4' },
    { key4: 'Test value 5', key5: 'Test value 6' }
  ]},

  { key1: 'Test value 11', key3: [ 
    { key4: 'Test value 13', key5: 'Test value 14' }, 
    { key4: 'Test value 15', key5: 'Test value 16' }
  ]},
]

const search = document.getElementById('search')
const matchList = document.getElementById('match-list')
searchStates = searchText => {
  const found = object.filter(obj => {
    return obj.key3.some(i => i.key4 == searchText)
  })
  matchList.textContent = JSON.stringify(found, null, 2)
}

search.addEventListener('input', () => searchStates(search.value))
<input type="text" id="search" class="form-control form-control-lg" placeholder="type here" />
<pre id="match-list"></pre>

And for matching values that starts with the search input value you can do this:

var object = [
  {
    key1: 'Test value 1',
    key3: [
      { key4: 'Test value 3', key5: 'Test value 4' },
      { key4: 'Test value 5', key5: 'Test value 6' },
    ],
  },

  {
    key1: 'Test value 11',
    key3: [
      { key4: 'Test value 13', key5: 'Test value 14' },
      { key4: 'Test value 15', key5: 'Test value 16' },
    ],
  },
]

const search = document.getElementById('search')
const matchList = document.getElementById('match-list')

searchStates = searchText => {
  if (!searchText) return (matchList.textContent = '')

  searchText = searchText.toLowerCase()
  const inputLength = searchText.length
  const found = object.filter(obj => {
    return obj.key3.some(
      i => i.key4.slice(0, inputLength).toLowerCase() == searchText
    )
  })
  matchList.textContent = JSON.stringify(found, null, 2)
}

search.addEventListener('input', () => searchStates(search.value))
<input type="text" id="search" class="form-control form-control-lg" placeholder="type here" />
<pre id="match-list"></pre>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!