0

I want to get a specific array based on a code, the array is something like this:

const arr = [
  [
    "https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=11f18ac1-0476-4a1e-ada6-09e6566abc19",
    1595619171842,
    "0b7ad06f-7776-4bab-a8c6-53fd5fd5bd9b"
  ],
  [
    "https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=b64c143d-e817-434f-bf6f-0bd0e8d9e7b5",
    1595619171844,
    "2f44a130-71d9-47ce-b5d5-04587c3c81fc"
  ],
  [
    "https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=71dc5d26-75f4-4141-905e-074b0705eac4",
    1595619171845,
    "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"
  ],
  [
    "https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=d3645614-0ea3-4d17-80ab-57c6c6525fab",
    1595619171846,
    "940fb9a7-6fdd-4f8b-a808-26a9c60114bf"
  ]
];

How to I get the array with code "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"? I was using reduce to get the more recent image, but now I have no idea!

0

2 Answers 2

3

Array#find returns the first elmenent of an array that returns true for the given function.

const specificArray = arr.find(subArray => {
     return subArray[2] === "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00";
}
Sign up to request clarification or add additional context in comments.

Comments

2

Array#find (with destructuring) is best suited for this purpose.

const res = arr.find(([,,code])=>code==="d7eb2a05-1f5a-48dd-b7ac-f3b071499d00");

const arr = [
  [
    "https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=11f18ac1-0476-4a1e-ada6-09e6566abc19",
    1595619171842,
    "0b7ad06f-7776-4bab-a8c6-53fd5fd5bd9b"
  ],
  [
    "https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=b64c143d-e817-434f-bf6f-0bd0e8d9e7b5",
    1595619171844,
    "2f44a130-71d9-47ce-b5d5-04587c3c81fc"
  ],
  [
    "https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=71dc5d26-75f4-4141-905e-074b0705eac4",
    1595619171845,
    "d7eb2a05-1f5a-48dd-b7ac-f3b071499d00"
  ],
  [
    "https://firebasestorage.googleapis.com/v0/b/ziro-a…=media&token=d3645614-0ea3-4d17-80ab-57c6c6525fab",
    1595619171846,
    "940fb9a7-6fdd-4f8b-a808-26a9c60114bf"
  ]
];
const res = arr.find(([,,code])=>code==="d7eb2a05-1f5a-48dd-b7ac-f3b071499d00");
console.log(res);

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.