0

having an array with objects and inside an options array how do I filter the inner array of objects by key value?

Here is the following example:

let test = [{
  "options": [{
     "label": "Audi",
     "value": 10
   },
    {
     "label": "BMW",
     "value": 18
   },
   {
     "label": "Mercedes Benz",
     "value": 116
   },
   {
     "label": "VW",
     "value": 184
   }
 ],
 "label": "test1"
},
{
 "options": [{
    "label": "Adler",
    "value": 3664
  },
  {
    "label": "Alfa Romeo",
    "value": 3
  },
  {
    "label": "Alpine",
    "value": 4
  }
],
 "label": "test2"
}
]

how do I get back the object:

 {
   "label": "Audi",
   "value": 10
 }

if I filter with keyword Audi

return label.toLowerCase().includes(inputValue.toLowerCase());

I tried with the following

test.map((k) => {
              res = k.options.filter((j) => {
                inputValue.toLowerCase();
                if (j.label.toLowerCase().includes(inputValue.toLowerCase())) {
                  return j;
                }
              });
            });
2
  • filter() is for creating a new array with elements that meet a condition. If you want to return an element that meets a condition, use find(). Commented Apr 21, 2020 at 19:26
  • You're not returning res. Commented Apr 21, 2020 at 19:30

5 Answers 5

1

You need to return the result of filter(), not just assign it to a variable, so that map() will return the results.

let test = [{
    "options": [{
        "label": "Audi",
        "value": 10
      },
      {
        "label": "BMW",
        "value": 18
      },
      {
        "label": "Mercedes Benz",
        "value": 116
      },
      {
        "label": "VW",
        "value": 184
      }
    ],
    "label": "test1"
  },
  {
    "options": [{
        "label": "Adler",
        "value": 3664
      },
      {
        "label": "Alfa Romeo",
        "value": 3
      },
      {
        "label": "Alpine",
        "value": 4
      }
    ],
    "label": "test2"
  }
]
let inputValue = "audi";
let search = inputValue.toLowerCase();
let result = test.map(k => k.options.filter(j => j.label.toLowerCase().includes(search)));
console.log(result);

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

1 Comment

thanks a lot, it works I just had to concat the result for my use case!
1

This will return all options matching the search query :

function find(array, query) {
  return array.reduce((prev, current) => prev.concat(current.options), []).filter(item => item.label.includes(query))
}

find(test, 'Audi')

Comments

1

Use flatMap() followed by filter():

let test=[{options:[{label:"Audi",value:10},{label:"BMW",value:18},{label:"Mercedes Benz",value:116},{label:"VW",value:184}],label:"test1"},{options:[{label:"Adler",value:3664},{label:"Alfa Romeo",value:3},{label:"Alpine",value:4}],label:"test2"}]

let result = test.flatMap(el => {
    return el.options.filter(car => car.label == "Audi")
})[0]

console.log(result)

Comments

1

You need to go two levels deep when traversing your array.

function filterArray(needle, haystack) {
  return haystack
    .map(h => h.options)
    .flatMap(h => h )
    .filter(h => h.label.toLowerCase().includes(needle.toLowerCase()));

CodeSandbox

1 Comment

Thanks for showing me flatMap(), didn't know that existed!
0

This might gile your answer:

console.log(test[0].options[0]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.