-3

I'm new to Arrays and objects so I was the whole day stuck at making this task I have a this JSON file or array lang (JavaScript) i was searching on StackOverflow also but can't understand them really well:

[
  { "date": "1959:01", "1": 138.89, "2": 139.39, "3": 139.74, "4": 139.69, "5": 140.68, "6": 141.17 },
  { "date": "1959:07", "1": 141.7, "2": 141.9, "3": 141.01, "4": 140.47, "5": 140.38, "6": 139.95 },
  { "date": "1960:01", "1": 139.98, "2": 139.87, "3": 139.75, "4": 139.56, "5": 139.61, "6": 139.58 },
  { "date": "1960:07", "1": 140.18, "2": 141.31, "3": 141.18, "4": 140.92, "5": 140.86, "6": 140.69 },
  { "date": "1961:01", "1": 141.06, "2": 141.6, "3": 141.87, "4": 142.13, "5": 142.66, "6": 142.88 },
  { "date": "1961:07", "1": 142.92, "2": 143.49, "3": 143.78, "4": 144.14, "5": 144.76, "6": 145.2 },
  { "date": "1962:01", "1": 145.24, "2": 145.66, "3": 145.96, "4": 146.4, "5": 146.84, "6": 146.58 },
  { "date": "1962:07", "1": 146.46, "2": 146.57, "3": 146.3, "4": 146.71, "5": 147.29, "6": 147.82 },
  { "date": "1963:01", "1": 148.26, "2": 148.9, "3": 149.17, "4": 149.7, "5": 150.39, "6": 150.43 },
  { "date": "1963:07", "1": 151.34, "2": 151.78, "3": 151.98, "4": 152.55, "5": 153.65, "6": 153.29 },
  { "date": "1964:01", "1": 153.74, "2": 154.31, "3": 154.48, "4": 154.77, "5": 155.33, "6": 155.62 },
  { "date": "1964:07", "1": 156.8, "2": 157.82, "3": 158.75, "4": 159.24, "5": 159.96, "6": 160.3 },
  { "date": "1965:01", "1": 160.71, "2": 160.94, "3": 161.47, "4": 162.03, "5": 161.7, "6": 162.19 },
  { "date": "1965:07", "1": 163.05, "2": 163.68, "3": 164.85, "4": 165.97, "5": 166.71, "6": 167.85 },
  { "date": "1966:01", "1": 169.08, "2": 169.62, "3": 170.51, "4": 171.81, "5": 171.33, "6": 171.57 },
  { "date": "1966:07", "1": 170.31, "2": 170.81, "3": 171.97, "4": 171.16, "5": 171.38, "6": 172.03 }
]

Those are like an income for a company for a specified number of years like in the beginning 1959:01 from 1 to 6 are the first 6 months of the year and the same year 1956:07 but diff in ":07" are the values of the last 6 months (1 to 6) of same year so I wanna make the output as follows with the easiest code to understand please.

  "1956": {
    "1": "value",
    "2": "value",
    "3": "value",
    "4": "value",
    "5": "value",
    "6": "value",
    "7": "value",
    "8": "value",
    "9": "value",
    "10": "value",
    "11": "value",
    "12": "value"
  },
  "1957": {
    "1": "value",
    "2": "value",
    "3": "value",
    "4": "value",
    "5": "value",
    "6": "value",
    "7": "value",
    "8": "value",
    "9": "value",
    "10": "value",
    "11": "value",
    "12": "value"
  },
  "1958": {
    "1": "value",
    "2": "value",
    "3": "value",
    "4": "value",
    "5": "value",
    "6": "value",
    "7": "value",
    "8": "value",
    "9": "value",
    "10": "value",
    "11": "value",
    "12": "value"
  }

Appreciate help quite a lot.

2
  • There is not enough detail here. In your output are the property names ("1"..."12") coming from the date in the input? Are they the properties with number names in the input? Also how is "value" calculated? Also, can there be duplicate values of "date" (same year and month?) in the input? Commented Nov 29, 2020 at 17:54
  • okay just focus with me on the first and second object cause it's same work for the others it's like when you go to 1956:01 you find six months each month has it's own income (value) this object must be concatenated with the other half of the year which is the 1956:07 to form months or numbers from 1 to 12 instead of being two objects having 6 each. Commented Nov 29, 2020 at 18:07

1 Answer 1

1

Here is your answer:

const data=[
  { "date": "1959:01", "1": 138.89, "2": 139.39, "3": 139.74, "4": 139.69, "5": 140.68, "6": 141.17 },
  { "date": "1959:07", "1": 141.7, "2": 141.9, "3": 141.01, "4": 140.47, "5": 140.38, "6": 139.95 },
  { "date": "1960:01", "1": 139.98, "2": 139.87, "3": 139.75, "4": 139.56, "5": 139.61, "6": 139.58 },
  { "date": "1960:07", "1": 140.18, "2": 141.31, "3": 141.18, "4": 140.92, "5": 140.86, "6": 140.69 },
  { "date": "1961:01", "1": 141.06, "2": 141.6, "3": 141.87, "4": 142.13, "5": 142.66, "6": 142.88 },
  { "date": "1961:07", "1": 142.92, "2": 143.49, "3": 143.78, "4": 144.14, "5": 144.76, "6": 145.2 },
  { "date": "1962:01", "1": 145.24, "2": 145.66, "3": 145.96, "4": 146.4, "5": 146.84, "6": 146.58 },
  { "date": "1962:07", "1": 146.46, "2": 146.57, "3": 146.3, "4": 146.71, "5": 147.29, "6": 147.82 },
  { "date": "1963:01", "1": 148.26, "2": 148.9, "3": 149.17, "4": 149.7, "5": 150.39, "6": 150.43 },
  { "date": "1963:07", "1": 151.34, "2": 151.78, "3": 151.98, "4": 152.55, "5": 153.65, "6": 153.29 },
  { "date": "1964:01", "1": 153.74, "2": 154.31, "3": 154.48, "4": 154.77, "5": 155.33, "6": 155.62 },
  { "date": "1964:07", "1": 156.8, "2": 157.82, "3": 158.75, "4": 159.24, "5": 159.96, "6": 160.3 },
  { "date": "1965:01", "1": 160.71, "2": 160.94, "3": 161.47, "4": 162.03, "5": 161.7, "6": 162.19 },
  { "date": "1965:07", "1": 163.05, "2": 163.68, "3": 164.85, "4": 165.97, "5": 166.71, "6": 167.85 },
  { "date": "1966:01", "1": 169.08, "2": 169.62, "3": 170.51, "4": 171.81, "5": 171.33, "6": 171.57 },
  { "date": "1966:07", "1": 170.31, "2": 170.81, "3": 171.97, "4": 171.16, "5": 171.38, "6": 172.03 }
]

const formated={};

for(let obj of data){
  const formatDate=obj['date'].split(":")[0];
  formated[formatDate]={...obj};
  delete formated[formatDate]['date']
}

console.log(formated)

Here the variable formated is an object that looks like this:

'1959': {
    '1': 141.7,
    '2': 141.9,
    '3': 141.01,
    '4': 140.47,
    '5': 140.38,
    '6': 139.95
  },
  '1960': {
    '1': 140.18,
    '2': 141.31,
    '3': 141.18,
    '4': 140.92,
    '5': 140.86,
    '6': 140.69
  },
  '1961': {
    '1': 142.92,
    '2': 143.49,
    '3': 143.78,
    '4': 144.14,
    '5': 144.76,
    '6': 145.2
  },
  '1962': {
    '1': 146.46,
    '2': 146.57,
    '3': 146.3,
    '4': 146.71,
    '5': 147.29,
    '6': 147.82
  },
  '1963': {
    '1': 151.34,
    '2': 151.78,
    '3': 151.98,
    '4': 152.55,
    '5': 153.65,
    '6': 153.29
  },
  '1964': {
    '1': 156.8,
    '2': 157.82,
    '3': 158.75,
    '4': 159.24,
    '5': 159.96,
    '6': 160.3
  },
  '1965': {
    '1': 163.05,
    '2': 163.68,
    '3': 164.85,
    '4': 165.97,
    '5': 166.71,
    '6': 167.85
  },
  '1966': {
    '1': 170.31,
    '2': 170.81,
    '3': 171.97,
    '4': 171.16,
    '5': 171.38,
    '6': 172.03
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah man absolutely gorgeous snippet of code i just need last step is to concatenate the first object with the second object so it's 12
I just need one more step is to concatenate every single object with the next object to form a full 12 months year like 1956:01 with the 1956:07 to make a whole year

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.