2

So I'm currently fetching data from an API but the data I'm getting is very weird. For example if I want to get the temperature, humidity and date from the API for 1 specific location I will get something like this:

data: {temp="15",humidity=null,date=null,lat=50,long=5}
data: {temp=null,humidity="7",date=null,lat=50,long=5}
data: {temp=null,humidity=null,date="20190417",lat=50,long=5}

So it seems the API fetches everything individual, but instead of grouping it all together and sending 1 array it sends an array for every piece of information I request. Is it possible to take the relevant data out of each array and put it into 1 for each request I do? I would like to do the grouping with Javascript or JQuery. I know normally you have to post what you tried, but honestly at the moment I'm at a complete loss, just a starting point on how to tackle the problem would be nice.

Expected result

data: {temp="15",humidity="7",date="20190417",lat=50,long=5}
5
  • 2
    Can you share the code where you are making the request to API and logging the response? Commented Apr 17, 2019 at 9:35
  • 2
    are those different JSON documents or array of objects? Commented Apr 17, 2019 at 9:35
  • @nightgaunt no, at the moment not. I only just tested the data through the API in the browser (and above is what I retrieve from my test). I will probably do it through a simple AJAX call. As I said I have no code to show at the moment. I'm just looking for a way to tackle the problem in the first place. Commented Apr 17, 2019 at 9:40
  • The reason I asked is because the solution will vary based on whether you make all three requests at once or one after another. If you make the requests one after another, below solutions are the best. Otherwise, you need to update your base result object upon receiving response. Commented Apr 17, 2019 at 9:43
  • @nightgaunt Ahh yes I see what you mean, the request will fetch all three at once. but I think I can manage to separate them and use Eddie's code to solve my problem. Thank for the info. Commented Apr 17, 2019 at 9:46

2 Answers 2

2

Use Object.entries to get entries from object, filter out null values, recreate object from entries (Object.fromEntries), merge all objects together (Object.assign)

const result = data.reduce((target_object, curr) => {
    const entries = Object.entries(curr);
    const filtered_entries = entries.filter(([key, value]) => value !== null);
    const filtered_object = Object.fromEntries(filtered_entries);
    Object.assign(target_object, filtered_object)
}, {});

const data = [{temp: "15", humidity: null, date: null, lat: 50, long: 5}, {temp: null, humidity: "7", date: null, lat: 50, long: 5}, {temp: null, humidity: null, date: "20190417", lat: 50, long: 5}];
const result = data.reduce((prev, curr) => Object.assign(prev, Object.fromEntries(Object.entries(curr).filter(([key, value]) => value !== null))), {});
console.log(result);

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

Comments

1

You can put the objects in one array and use reduce loop thru the array. Convert each object into an array using Object.keys and loop using forEach

let data1 = {temp:"15",humidity:null,date:null,lat:50,long:5}
let data2 = {temp:null,humidity:"7",date:null,lat:50,long:5}
let data3 = {temp:null,humidity:null,date:"20190417",lat:50,long:5}

let result = [data1, data2, data3].reduce((c, v) => {
  Object.keys(v).forEach(o => c[o] = c[o] || v[o]);
  return c;
}, {});

console.log(result);

1 Comment

Thanks, I did a quick test with the data I retrieved and it worked like a charm!

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.