0

using Angular - I have a json file with an array people[], which has an array phones[]

I want to return

people[index].phones[index].phonenumber

(where people.personid = x and people.phones.phoneid = y) 

Any suggestions are greatly appreciated

3
  • Can the peopleId and phoneId be used as indieces of the people array and phones array respectively? Or are both these arrays array of objects? Commented Nov 27, 2018 at 18:15
  • 1
    Are you looking to get every single phone number all of the people? Or, are you looking at getting a list of phone numbers for each person? Or, are you wanting to get a list of phone numbers for a specific person (I think this is what @SiddAjmera was looking for with his question)? Commented Nov 27, 2018 at 18:16
  • phones[] is an array of phone numbers - so I am looking for a specific phone number of a type phonetypeID. i.e., when phonetypeID = 1, the associated phonenumber is the office number. Every person has several phone numbers. So, to create a list of office numbers for each person I need per personID, phonenumber where phonetypeID = 1 Commented Nov 28, 2018 at 17:04

2 Answers 2

1

Simply use the filter function on the arrays.

let person = people.filter(person => person.personId === x);
let phone = person && person.phones.filter(phone => phone.phoneId === y);

Here's a Working Example as a Sample StackBlitz for your ref.

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

2 Comments

This is unnecessarily complex. Why not just access the index of the array directly?
Can you please read the question again? index has nothing to do with the fields the user has in there. OP wants to get the person and phone based on personId and phoneId which could be different from the indices of the Objects in the respective arrays.
0

You can use a filter function which returns a filtered array

private filterPhone(x:number,y:number) : number { 
    let phoneNum : number;
    let person = this.people.filter(person => person.personId === x);
    let phone = person.length > 0 && person[0].phones.filter(phone => phone.phoneId === y);
    if(phone.length > 0 && phone[0].phoneNumber){
      phoneNum = phone[0].phoneNumber;
    }
    return phoneNum;
}

Or you could use a findIndex() function for achieving this.

private findPhoneById(x:any,y:any) : number { 
    let result : number = -1; 
    let personIndx = this.people.findIndex(p => p.personId === x);
    if(personIndx > result){
        let phoneIndx = this.people[personIndx].phones.findIndex(phone => phone.phoneId === y);
        if(phoneIndx > result){
          result = this.people[personIndx].phones[phoneIndx].phoneNumber;
        }
    }
    return result;
}

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.