0

I want to extract 10 ids from and object "objectIDs" and put them into an array "idArr". "objectIDs" is an object I get when fetching data (code bellow). The problem is that "objectIDs" is an array? objectIDs[ 0:[array of ids which I want to extract] 1:[another array of ids] 2:[...] .... ] I want to pass 10 of the 0th index ids from objectIDs but dont seem to be able to as the "idArr" I pass them to looks like [0:ID,1:id,...,10:ID]. And I can`t seem to make use of that array. Would like to find a way to pass the ids from "objectIDs" to "objArr" in the format [ID,ID,ID...,ID]. Is that possible?

let idArr = [];

function getObjectIds() {
  fetch("https://collectionapi.metmuseum.org/public/collection/v1/search?hasImages=true&q=early renaissance")
    .then(response => response.json())
    .then((data) => {
      console.log(data);
      for (let i = 0; i < 10; i++) {
        idArr.unshift(data.objectIDs);
      }
    })
}

getObjectIds();
console.log(idArr);

Sample data from the link:

{"total":1613,"objectIDs":[199003,22369,207034,208555,436918,390196,387111,480645,460738,459129,206912,437203,21126,10705,12569,438688,207010,75586,386903,363020,437245,468639,436666,468166,696866,386797,386933,386934,35842,23144,436989,196207,196205,196206,436988,195186,461131,460494,459246,25396,193617,21974,206757,462776,505681,437912,813840,437372,436051,436101,201862,459088,437059,193625,193626,186124,505501,460490,468716,467746,204722,472562,503517,186170,471933,471934,337061,197033,195447,437593,447570,468165,201398,202389,202388,459072,473871,676458,428525,236192,460611,23229,210185,191486,460498,25562,25486,25528,25444,207394,458978,467760,435728,201910,445297,5223,195190,468431,203921,201802,460832,460645,207257,474336,435595,202970,201375,192912,437007,195054,197757,471931,471938,471936,471935,471937]}
adiga
  • 34,372
  • 9
  • 61
  • 83
KChimev
  • 11
  • 1

3 Answers3

1

Array.slice creates a shallow copy of your original array, with the parameters standing for start index and end index.

const data = {"total":1613,"objectIDs":[199003,22369,207034,208555,436918,390196,387111,480645,460738,459129,206912,437203,21126,10705,12569,438688,207010,75586,386903,363020,437245,468639,436666,468166,696866,386797,386933,386934,35842,23144,436989,196207,196205,196206,436988,195186,461131,460494,459246,25396,193617,21974,206757,462776,505681,437912,813840,437372,436051,436101,201862,459088,437059,193625,193626,186124,505501,460490,468716,467746,204722,472562,503517,186170,471933,471934,337061,197033,195447,437593,447570,468165,201398,202389,202388,459072,473871,676458,428525,236192,460611,23229,210185,191486,460498,25562,25486,25528,25444,207394,458978,467760,435728,201910,445297,5223,195190,468431,203921,201802,460832,460645,207257,474336,435595,202970,201375,192912,437007,195054,197757,471931,471938,471936,471935,471937]};

const first10 = data.objectIDs.slice(0, 10);

console.log(first10);
Nora Söderlund
  • 1,148
  • 2
  • 18
0

If I understood problem correctly: input: [[1,7,5...8], [],..... []] output: [1,7,5...8]

Try below code:

 function getObjectIds(){
   ....
   const idArr = objectIDs[0];
   return idArr;
}
maddy
  • 1
  • 3
  • So it seems as I tweaked the code, data.objectIDs[i] returns a number which is correct. But when I try to push IDs idArr.push(data.objectIDs[i]) to fill the idArr with "i" number of IDs from data.objectIDs, idArr[i] returns undefined? – KChimev Dec 29 '22 at 10:44
  • As javascript is asynchronous your `console.log()` runs before `.then()` – maddy Dec 29 '22 at 10:46
  • I noticed that if "idArr" is declared inside the fetch function the numbers are pushed properly and can be logged but is there a way to use "idArr" outside the fetch if it is defined inside the fetch function? I apollogise for the lack of knowledge on the matter. – KChimev Dec 29 '22 at 10:50
  • There are 2 options: 1. Move `console.log()` inside `.then()` and after populating `idArr` 2. Or use async await – maddy Dec 29 '22 at 10:53
  • Thanks a lot for the help. I will try both ways and see which suits me better. I`m still learning to use fetch and asynchronous functions so that explanation really helps. – KChimev Dec 29 '22 at 10:55
0

let idArr = [];

function getObjectIds() {
  fetch("https://collectionapi.metmuseum.org/public/collection/v1/search?hasImages=true&q=early renaissance")
    .then(response => response.json())
    .then((data) => {
      console.log("real dta", data);
        idArr = data.objectIDs.slice(0, 10);
      console.log("id arr", idArr);
    });
}

getObjectIds();
Saima Haji
  • 185
  • 1
  • 5