1

I'm pretty new to learning to code. So i might get a lot of basics wrong.

Basically i am downloading API content from two different accounts via request-promise and want to merge them into a bigger array. I'm struggling with escaping my local data from the request-promise function and also combining it with the second array

Here's what i got so far:

//request the site and do some stuff with the data
rp(rpOptions)
    .then(function (parsedBody) {
        let incomingData1 = (parsedBody); //turning data into a value to change it a little
        incomingData1.forEach((incomingData1) => {incomingData1.yearsRetired = 0}); //to add a new property
        incomingData1 = JSON.stringify(parsedBody, ["favFood", "age", "work", "yearsRetired"], 2); //to filter only relevant properties into a JSON thing (i eventually want to save it to a txt file)
});

i'd then do the same for the second account and then try to get that data outside of the function and merge it into a single array so that it looks like this:

{
  "first_account_name": {
    "individual1": {
      "favFood": 'fries', 
      "age": 23,
      "work": 'astronaut'
      "yearsRetired": 0
    },
    "individual2": {
      "favFood": 'banana', 
      "age": 55,
      "work": 'zookeeper'
      "yearsRetired": 0
    {
      ...
    }
  },
  "second_account_name": { ... }
    "individual6": {
      "favFood": 'apple', 
      "age": 49,
      "work": 'dinosaur'
      "yearsRetired": 0
    "individual7": {
      "favFood": 'sausage', 
      "age": 33,
      "work": 'doctor'
      "yearsRetired": 0
    {
      ...
}

how do i get my data into a variable outside of rp? and how do i set it up so that it ends up like a nested array?

Thanks a lot and sorry for being confusing :P

lurf
  • 23
  • 2

2 Answers2

0

What you are looking for is a global array that gets data pushed into it on every Promise request called right. So firstly, create a simple array and place it on top of the page or if you are using a class just insert it into the respective fields.

Let accountDetails = [];

Next, inside then function call this variable like so,

rp(rpOptions)
    .then(function (parsedBody) {
        let incomingData1 = (parsedBody);
        incomingData1.forEach((incomingData1) => {incomingData1.yearsRetired = 0}); 
        incomingData1 = JSON.stringify(parsedBody, ["favFood", "age", "work", "yearsRetired"], 2);
        accountDetails.push({
              "individual1" : incomingData1 
        })
});
Mr Khan
  • 2,139
  • 1
  • 7
  • 22
0

If you're using ES6

const processData = (data) => {
  return data.map((item) => ({
    favFood: item.favFood,
    age: item.age,
    work: item.work,
    yearsRetired: 0
  }))
}


// any value returned by then will be wrapped in promise 
// and can be `await` ed
// you can also use 
// const [ data1, data2 ] = await Promise.all([
//  rp(requestOption1).then(data => processData(data)),
//  rp(requestOption2).then(data => processData(data))
// ]) 
// if you want it to be executed parallely
const data1 = await rp(requestOption1).then(data => processData(data));
const data2 = await rp(requestOption2).then(data => processData(data));


const mergedData = [
  ...data1,
  ...data2
];

If you don't have async await

const processData = (data) => {
  return data.map((item) => ({
    favFood: item.favFood,
    age: item.age,
    work: item.work,
    yearsRetired: 0
  }))
}

Promise.all(
  rp(requestOption1).then(data => processData(data)),
  rp(requestOption2).then(data => processData(data))
).then(results => {
  const mergedData = results.reduce((collection, result) => {
    return collection.concat(result);
  }, []);
})

Note:

  • I wrote the function name processData because I don't know what is being processed. I suggest you to be more specific on the function name. (e.g. what it does)