-3

So I have a large object thats is similar in structure to this:

var obj = {
      '1': {
        name: "Naruto Uzumaki",
        series: "Naruto"
      },
      '2': {
        name: "Sasuke Uchiha",
        series: "Naruto"
      },
      '3': {
        name: "Edward Elric",
        series: "Fullmetal Alchemist"
      },
      '4': {
        name: "Alphonse Elric",
        series: "Fullmetal Alchemist"
      }
    }

I'd like to know how I can search via name or series and return the ids of the responses. For example:

//I search for Fullmetal Alchemist it should return this:
['3', '4']

//I search for Naruto Uzumaki it returns
['1']

//I search for Elric it returns
['3', '4']

//And I search for Naruto it returns
['1', '2']

So basically im looking for a search function to look through all of the nested objects.

I'd also like to note im doing this in a nodejs enviroment.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Keem ROH
  • 3
  • 2
  • 1
    Hint: `Object.keys(obj).filter(...)` – Barmar Jul 12 '21 at 21:28
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods) methods (both static and on prototype). – Sebastian Simon Jul 12 '21 at 21:29
  • Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – Benedikt Kromer Jul 12 '21 at 21:30

2 Answers2

0

You can use Object.entries To map an object to array of [key, value] pairs. Then you can perform array actions like filter and finally map to return array of keys instead of array of objects.

To not write only a specific search function you can create a function with a callback which I named here matcher - it is a matchin function that will get each object from an array as an argument.

var obj = {
      '1': {
        name: "Naruto Uzumaki",
        series: "Naruto"
      },
      '2': {
        name: "Sasuke Uchiha",
        series: "Naruto"
      },
      '3': {
        name: "Edward Elric",
        series: "Fullmetal Alchemist"
      },
      '4': {
        name: "Alphonse Elric",
        series: "Fullmetal Alchemist"
      }
    }
    
    function search(matcher) {
      return Object.entries(obj)
        .filter(([key, value]) => matcher(value))
        .map(([key]) => key)
    }
    
    console.log(
      search(value => value.series.match(value.series.match('Fullmetal Alchemist')))
    )
Hakier
  • 505
  • 2
  • 13
0

try this code

var obj = {
  1: {
    name: 'Naruto Uzumaki',
    series: 'Naruto',
  },
  2: {
    name: 'Sasuke Uchiha',
    series: 'Naruto',
  },
  3: {
    name: 'Edward Elric',
    series: 'Fullmetal Alchemist',
  },
  4: {
    name: 'Alphonse Elric',
    series: 'Fullmetal Alchemist',
  },
};

function searchByNameOrSeries(object, nameOrSeries) {
  const results = [];

  for (const property in object) {
    if (
      object[property].name.includes(nameOrSeries) ||
      object[property].series.includes(nameOrSeries)
    ) {
      results.push(property);
    }
  }

  return results;
}

console.log(searchByNameOrSeries(obj, 'Elric'));
console.log(searchByNameOrSeries(obj, 'Naruto'));
Abde Laziz
  • 119
  • 7