anshul khandelwal
anshul khandelwal

Reputation: 7

How to Search Data from a array of objects (where object have multiple key and value as array)?

For example

const data = [
    {
      companies: [
        {name: 'Yuri'},
        {name: 'Boeing'},
        {name: 'Laser'},
      ],
      sectors: [
        {name: 'Logistic'},
        {name: 'Aviation'},
        {name: 'Vocal'},
      ],
      location: [
        {name: 'Hong'},
        {name: 'Singapore'},
        {name: 'Switzerland'},
      ],
    },
  ];

if a text is searched as 'vocal' how can we search and return value in same format.

Upvotes: 1

Views: 60

Answers (5)

MikeM
MikeM

Reputation: 13641

It is not clear to me exactly what you want to be returned, but here is one way to search, within an object, for an object containing a particular case-insensitive string value, not matter how deeply the object is nested.

const search = (object, string) => {
  for (const value of Object.values(object)) {
    if (typeof value === 'object') {
      const result = search(value, string); 
      if (result !== undefined) return result;
    } else if (typeof value === 'string') {
      if (value.toLowerCase() === string.toLowerCase()) return object;
    }  
  }  
};

search(data, 'vocal');    // { name: "Vocal" }

Upvotes: 0

Ivan86
Ivan86

Reputation: 5708

You can use a for-in loop to go through the object keys, and a for-of to go through the array of objects:

const data = [
  {
    companies: [
      {name: 'Yuri'},
      {name: 'Boeing'},
      {name: 'Laser'},
    ],
    sectors: [
      {name: 'Logistic'},
      {name: 'Aviation'},
      {name: 'Vocal'},
    ],
    location: [
      {name: 'Hong'},
      {name: 'Singapore'},
      {name: 'Switzerland'},
    ],
  },
];

function search() {
    let searchVal = document.getElementById('search').value.toLowerCase().trim();
    for (let key in data[0]) {
        for(obj of data[0][key]) {
            if(searchVal == obj.name.toLowerCase()) {
                console.log(obj);
                return;
            }
        }
    }
    console.log("Item was not found.");
}
<input type="text" id="search" value="" />
<input type="button" value="Search" onclick="search()" />

Upvotes: 0

wittgenstein
wittgenstein

Reputation: 4482

Here is one of many solutions. You have to loop over your data, and filter every entries by their key. It's important to do a .toLowerCase() Comparison.

const data = [
  {
    companies: [
      { name: 'Yuri' },
      { name: 'Boeing' },
      { name: 'Laser' },
    ],
    sectors: [
      { name: 'Logistic' },
      { name: 'Aviation' },
      { name: 'Vocal' },
    ],
    location: [
      { name: 'Hong' },
      { name: 'Singapore' },
      { name: 'Switzerland' },
    ],
  },
];

const query = 'vocal'
let result = {}
Object.keys(data[0]).forEach(key => {
  data[0][key].filter(str => {
    if (str.name.toLowerCase() === query.toLowerCase()) {
      result = str
    }
  })
})

console.log(result) // { name: 'Vocal'}

Upvotes: 0

Alen.Toma
Alen.Toma

Reputation: 4870

This should not be to hard.

Use reduce and filter

See example below

    const data = [
    {
      companies: [{
          name: 'Yuri'
        },
        {
          name: 'Boeing'
        },
        {
          name: 'Laser'
        },
      ],
      sectors: [{
          name: 'Logistic'
        },
        {
          name: 'Aviation'
        },
        {
          name: 'Vocal'
        },
      ],
      location: [{
          name: 'Hong'
        },
        {
          name: 'Singapore'
        },
        {
          name: 'Switzerland'
        },
      ],
    }];

    var searchText = "Vocal".toLowerCase();
    var result = data.reduce((arr, value) => {
      var item = {}
      const search=(key)=>{
          item[key] = value[key].filter(x => x.name.toLowerCase().indexOf(searchText) != -1);
      }
     search("companies")
     search("sectors")
     search("location")
     arr.push(item)
      
      return arr;
    }, [])

    console.log(result)

Upvotes: 1

Dag Eikesdal
Dag Eikesdal

Reputation: 43

I would just loop over and filter. Maybe something like this: Search a JavaScript object

It's just a loop on data array and then a loop on each item + return parent if found.

Upvotes: 0

Related Questions