-1

I need a list in the following Format in Javascript to populate my graph:

    var songs = 
    {
    "Mon": 80,
    "Tues": 40,
    "Wed": 60,
    "Thu": 80,
    "Fri": 40,
    "Sat": 60,
    };

I am getting my data from my API which comes back in a list in the format of:

enter image description here

How can I loop through my list and adapt it to the form of the "songs" list?

Outcome:

var outputList = {
      "19 Jan 2021": 2, 
      "20 Jan 2021" : 5
....
}
  • It's weird to call a plain object with apparently fixed keys ... a list. It's not clear what you want to do. You could clarify by providing example input and corresponding output for that input. – trincot Jan 26 '21 at 16:11
  • what do you expect in list? i dont get it. please update this part. – 王仁宏 Jan 26 '21 at 16:13
  • paired key and value? – 王仁宏 Jan 26 '21 at 16:14
  • In modern JavaScript, there's typically no need for `$.each()` (JavaScript arrays have native iteration features) or `var` (use `let` or `const`). – jarmod Jan 26 '21 at 16:14
  • or just an object, description as key and value as its value? – 王仁宏 Jan 26 '21 at 16:15
  • I apologize if this is confusing. I found a template for a graph on Codepen, and the input data that they use to display their graph is the songs list that I posted at the top. I am retrieving my data in the form that I posted the picture of. How can I get my data into the form that they are expecting? – Emile v Rooyen Jan 26 '21 at 16:22
  • Please don't post code or text as image. – Thomas Sablik Jan 26 '21 at 16:27
  • please give some more clarity about the problem – nTheta Jan 26 '21 at 16:29
  • you seem to be wanting to create a list as an object. Really your intended output should be: var outputList = [{"Date": 19 Jan 2021, "Value" : 2}, "Date": 20 Jan 2021, "Value":5}] Does that make sense to you? – markthewizard1234 Jan 26 '21 at 16:29
  • @markthewizard1234 Yes, that makes sense. So how do I declare such a list and loop through my list and append the list? – Emile v Rooyen Jan 26 '21 at 16:31
  • 1
    What have you tried so far? Please show us your research and effort. Please ask only one question per question. Do you want to know how to loop through your "list" or how to adapt it to the form of the "songs" list? [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) _"Asking a question on Stack Overflow should be the last step in your process for finding an answer"_ – Thomas Sablik Jan 26 '21 at 16:34
  • I don't understand what is the expected outcome. the ```songs``` variable only holds for key the name of the day, while the ```outputList``` holds a ```'dd MM YYYY'``` string for key – grodzi Jan 26 '21 at 16:35

1 Answers1

0

If I understood your problem properly the following should solve it:

let apiResponse = [
  {description: '18 Jan 2021', value: 1},
  {description: '19 Jan 2021', value: 2}
]
let outputList = {};
apiResponse.forEach((row) => {
  outputList[row.description] = row.value;
});
console.log(outputList);
farhodius
  • 510
  • 3
  • 8
  • Please explain your answer. You should try it yourself first. Please don't post code-only answers. – Thomas Sablik Jan 26 '21 at 16:38
  • I tried and it works. I'm looping through the api response and populating an object the way @Emile v Rooyen is asking. Can you now explain why I'm getting downvote? – farhodius Jan 26 '21 at 16:40
  • You should explain your solution especially if OP doesn't provide any code. Why would you write _"Try this:"_ if you know that it works? The reason for the downvote is the missing description. – Thomas Sablik Jan 26 '21 at 16:42
  • Do you like it better now? With all due respect I think the intro phrase to the solution should not be the reason for downvote. But based on your reputation you obviously know better. – farhodius Jan 26 '21 at 16:48
  • You should explain your code. Obviously OP is an absolute beginner and doesn't know how to implement simple algorithms. A code without description leads to Cargo Cult Programming. OP can copy and paste your code but won't understand it. – Thomas Sablik Jan 26 '21 at 16:49
  • Sounds good, thank you for the feedback . – farhodius Jan 26 '21 at 16:55
  • This worked perfectly! Thank you :) Sorry if the question was confusing. I had no idea how to work with lists in JavaScript – Emile v Rooyen Jan 26 '21 at 17:02