0

I got problem to get specific object in array.

Example array

enter image description here

I just want id

Code

for (let item of this.commonService.popularList$.getValue()) {
        console.log(item['menuList'])
}

if I add code like this console.log(item['menuList'][id]) it display like this undefined

3
  • 1
    Probably meant item['menuList'][0]['id'] or even item['menuList'][0].id (unless I misunderstood something) Commented Sep 20, 2020 at 13:44
  • if I add [0] it just select one and if more than one? what should I do?- Commented Sep 20, 2020 at 13:46
  • Is popularList$ on Observable? That's usually what the $ suffix indicates. Commented Sep 20, 2020 at 14:04

5 Answers 5

3

It appears you're accessing an array of objects.

Given the following:

const arr = [{ id: 12 }, { id: 57 }];

You can access the objects by index and then retrieve the array, the following example will provide you the id of the first item in the list (adjust 0 to whichever index you desire.)

arr[0].id

Or you could map the array to an array of simply ids like such...

arr.map(({ id }) => id)

Which would return a new array, in this example, this would equal:

[12, 57]
Sign up to request clarification or add additional context in comments.

Comments

2

If item['menuList'] is an array of objects then you need to put in the index as well

for (let item of this.commonService.popularList$.getValue()) {
    console.log(item['menuList'])
    for(let i=0; i<item['menuList'].length; i++){
         console.log(item['menuList'][i]['id']);//will print the ids
    }
}

Comments

2
let idArray = [];
for (let item of this.commonService.popularList$.getValue()) {
     if(item && item['menuList'] && Array.isArray(item['menuList']))
       item['menuList'].forEach(menu => idArray.push(menu.id));
}
 console.log(idArray)

Test code

let data = [{menuList:[{id:1},{id:2}]},{menuList:[{id:3},{id:4}]}];
let idArray = [];
for (let item of data) {
    if(item && item['menuList'] && Array.isArray(item['menuList']))
       item['menuList'].forEach(menu => idArray.push(menu.id));
}
console.log(idArray)

Comments

2

It appears that this.commonService.popularList$ is an RxJS BehaviorSubject, using its GetValue()/value members may not be desirable because we will not receive any new values that are published by the BehaviorSubject.

Therefore, I will suggest an alternative to what has been suggested so far.

this.commonService.popularList$.subscribe(popularList => {
  for (const item of popularList) {
    console.log(item.id);
  }
});

This means we will always print the latest values as they come in.

Comments

2

For retrieve the id that you want, just do the following:

const arr = [{ id: 9 }, { id: 15 }, { id: 18 }, { id: 25 }];
const specificId = 18;
const yourId = arr.find(({id}) => id === specificId);

console.log(yourId)

stackblitz of how it works.

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.