2

I have following array:

[
  {
    fileName: "test.pdf",
    fileUrl: "https://test-url1.com/test.pdf",
    isDeleted: true
  },
  {
    fileName: "test.pdf",
    fileUrl: "https://test-url1.com/test.pdf",
    isDeleted: true
  }
]

Now I need to create new array from this array which will contain only fileUrl data.

So that new array will have only fileUrl node and all it's links.

Should I use map OR filter method, or any other suggestion for this.

Thanks

1
  • please add a code snippet instead of code screenshots Commented Mar 2, 2023 at 10:28

1 Answer 1

2

You should use map for this.

const urlArray = array.map((item: any) => item.fileUrl);

// [ "https://test-url1.com/test.pdf", "https://test-url1.com/test.pdf"]

The rule of thumb is if you want to change the structure of the array you use map, if you want to get a subset of the array you use filter.

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

1 Comment

I tried it but I am getting this error : 'item' is of type 'unknown'.ts(18046)

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.