-2

I am trying to fetch NAME which is in SKILLS.

enter image description here

For that I use filter for initial level sorting.Means I am able to sort rows but how do I fetch name?

   let r = this.topics.filter(a => {
          console.log('a is : ', a)
          return a.Skills.name
          
        })
Jazz
  • 73
  • 7
  • `filter` is expected to filter.. maybe you meant `map` – Diego D Feb 03 '23 at 13:52
  • @DiegoD also doesn't sort – VLAZ Feb 03 '23 at 13:52
  • @VLAZ good point, my brain discarded that part while reading the question.. so `filter` is totally out of place. You should first `sort` the list and then use `map` over it but surely not `filter` – Diego D Feb 03 '23 at 13:55
  • @DiegoD by using map how will I be able to sort name? – Jazz Feb 03 '23 at 13:56
  • what is `this.topics`? – Tachibana Shin Feb 03 '23 at 13:57
  • 2
    To be quite honest, I'm not sure what should be happening. The code uses `.filter()` on `this.topics` yet the screenshot shows there is something called `rows`. The question also talks about sorting but not sure if it's relevant. Not really sure what's supposed to happen here. I'm mostly confident it's a duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/q/19590865) but maybe there are more things needed (which are almost assuredly duplicates, as well). – VLAZ Feb 03 '23 at 13:58
  • Do you want to extract skills from the array or sort the array by name? – Balaji Feb 03 '23 at 13:59
  • @Balaji extract name from skills – Jazz Feb 03 '23 at 14:00

2 Answers2

0

You can use Map and Sort functions to achieve this.

const topics = [{
  Skills: [{
    name: "Java",
    id: 1
  },
  {
    name: "C++",
    id: 2
  },
  {
    name: "Python",
    id: 3
  }
]
}]

let r = topics.map((a) => {
  return a.Skills.sort((a, b) => {
    return a.name.localeCompare(b.name);
  });
});

console.log(r);
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37
0

I think you are misunderstanding what Array.filter does, it filters out/removes items from the array that does not match the criteria the user passes it, more about it here filter docs

In this case you use a Array.map to transform the data, more about it heremap docs.

 let r = this.topics.map(item => {
      return item.skills.map( skill => skill.name)
 })

The above code will return something like [['Writing', 'Reading'], ['Testing', 'Debugging']]

Balaji
  • 795
  • 1
  • 2
  • 10