1

I am trying to pull data from an array, but I just want to pull the id field, not the name field as well. Using Json Stringify I can log the result in the console, here is the code and the result:

Console log:

console.log(JSON.stringify(this.selectedCustomers));

Result:

[{"id":1,"name":"John"},{"id":2,"name":"Jane"}]

The desired result should be this: [1,2]

I tried the following thinking it would filter the result:

console.log(JSON.stringify(this.selectedCustomers.id));

But I got the following error message: "Property 'id' does not exist on type 'any[]'"

How do I strip out the name field and just pull the ids? If anyone can tell me what i'm missing or how to fetch the id only it would be highly appreciated! Thanks!

1 Answer 1

4

You need to use map method and destructuring.

let result = this.selectedCustomers.map(({id}) => id);

Short example:

let arr = [{"id":1,"name":"John"},{"id":2,"name":"Jane"}]
console.log(arr.map(({id}) => id));

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

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.