1

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.

0

5 Answers 5

1

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)

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

Comments

0

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.

Comments

0

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'}

Comments

0

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()" />

Comments

0

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" }

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.