-3

I have to do something like left join in sql in node with JSON data. Actually, on componentWillReceiveProps react (if it's changing anything).

my state:

const dayList = {
  "2017-11-08": [],
  "2017-11-09": [],
  "2017-11-10": [],
  "2017-11-11": [],
  "2017-11-12": [],
  "2017-11-13": []
}

my data to join:

const visit = {
  "2017-11-11": "10:30",
  "2017-11-12": "10:00",
  "2017-11-12": "10:30"
}

And in componentdidmount i need to setState so that as result get that:

const dayList = {
  "2017-11-08": [],
  "2017-11-09": [],
  "2017-11-10": [],
  "2017-11-11": ["10:30"],
  "2017-11-12": ["10:00","10:30"],
  "2017-11-13": []
}

I know how to achieve it with .map, and another loop inside with if statement. But I am sure that there is a better approach. What may you suggest?

halfer
  • 19,824
  • 17
  • 99
  • 186
Kamil Lewandowski
  • 396
  • 1
  • 5
  • 15
  • There's no JSON in the above. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Nov 08 '17 at 11:57
  • 3
    Your `visit` initializer results in an object with only **two** properties, not three. (The second property named `"2017-11-12"` overwrites the first.) So the first thing you have to do is fix that. Until/unless you do, we can't help you, because the starting data structures can't do what you've described. – T.J. Crowder Nov 08 '17 at 11:58
  • FWIW: Stack Overflow is a **very** active place. When you post a question (or answer), *stick around* for a few minutes so you can address requests for clarification such as the one above. As of when I posted that, there were *at least* two people here actively trying to help you (probably more). Once the question gets stale, it's less likely to have helpful folks looking at it. – T.J. Crowder Nov 08 '17 at 12:13

1 Answers1

0

Yes, actually you are right. I answered the wrong question. Is not a valid JSON neither.

But still, there is a case to do. My very raw data from db looks like that:

[
  {
    "createDate": "Tue Nov 07 2017 20:56:27 GMT+0100",
    "visitDate": "20171110",
    "visitTime": "1030",
    "doctor": {
      "name": "name1"
    }
  },
  {
    "createDate": "Tue Nov 07 2017 20:56:36 GMT+0100",
    "visitDate": "20171111",
    "visitTime": "1000",
    "doctor": {
      "name": "name1"
    }
  },
  {
    "createDate": "Tue Nov 07 2017 23:30:03 GMT+0100",
    "visitDate": "20171111",
    "visitTime": "1030",
    "doctor": {
      "name": "name1"
    }
  }
];

and in react component and need to display it as a list of visits per day like that

  "2017-11-08": [],
  "2017-11-09": [],
  "2017-11-10": ["10:30"],
  "2017-11-11": ["10:00","10:30"],
  "2017-11-12": []
  "2017-11-13": []
Kamil Lewandowski
  • 396
  • 1
  • 5
  • 15