-1

I am trying to create an associative array of dates and style. I am expecting an array like

{   
   dates: [{day: 1, month: 12, year: 2021}, {day: 15, month: 12, year: 2021}],
   styleClass: 'test'
},

And my code is

var markedDates = [];
markedDates['dates'].push('day: 1, month: 12, year: 2021');
markedDates['dates'].push('day: 15, month: 12, year: 2021');
markedDates['styleClass'].push('test');
console.log(markedDates);

which returns error

geeth
  • 704
  • 2
  • 14
  • 42
  • 1
    `styleClass` also has to be an array: `styleClass: ['test']` – Hao Wu Dec 13 '21 at 09:35
  • for pushing, you need an array. [`Array#push`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) – Nina Scholz Dec 13 '21 at 09:35
  • 1
    markedDates should be defined as {}. And why you're not just copying the source in the top of your question confuses me. – Jack Bashford Dec 13 '21 at 09:35
  • 2
    And you're currently pushing strings, not objects. – pilchard Dec 13 '21 at 09:35
  • 2
    Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and, if needed, use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Dec 13 '21 at 09:36
  • 2
    You probably come from PHP background - please remember that in JavaScript, and most other languages, there is no such thing as "associative array". This is just an object. – Robo Robok Dec 13 '21 at 09:36
  • try defining markedDates as `var markedDates = {dates:[],styleClass:[]};` at least it would fix your error. And you can start restructuring your array/object. – Mukyuu Dec 13 '21 at 09:40

2 Answers2

2

Problem 1: markedDates has been defined as an array ([]). To define it as an object (which is what you have as your expected output), use {}. (No such thing as an 'associative array' in JavaScript; arrays use 0, 1, etc. as indexes. Objects use 0, 1, key, another_key as indexes [or keys, more accurately].)

Problem 2: You haven't defined the dates array within markedDates which will cause a reference error.

Problem 3: You are pushing strings to the array instead of objects. This won't error, but won't produce the intended output.

Problem 4: You're trying to use styleClass as an array - it's just a string property within the markedDates.

To fix your code in the line-by-line manner (keeping similarity to your original code):

var markedDates = {};

markedDates.dates = [];
markedDates.dates.push({day: 1, month: 12, year: 2021});
markedDates.dates.push({day: 15, month: 12, year: 2021});
markedDates.styleClass = "test";

console.log(markedDates);
.as-console-wrapper { max-height: 100% !important; top: auto; }

But if you've got your expected output statically defined at the top, you can just assign that directly to the variable as such:

var markedDates = {
  dates: [{ day: 1, month: 12, year: 2021 }, { day: 15, month: 12, year: 2021 }],
  styleClass: 'test'
};

console.log(markedDates);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

Things that you should consider -

  1. Make an object that needs to be pushed into the bigger array. Eg: markedDate in the below code.
  2. Assigning a string should be directly with an equal to operator and no need to push it, like in an array
  3. Initialize the internal field dates as an array before pushing into it first

var markedDates = [];
var markedDate = {};
markedDate['dates'] = [];
markedDate['dates'].push({day: 1, month: 12, year: 2021});
markedDate['dates'].push({day: 15, month: 12, year: 2021});
markedDate['styleClass'] = 'test';
markedDates.push(markedDate);
console.log(markedDates);
Vandesh
  • 6,368
  • 1
  • 26
  • 38
  • Don't encourage using `markedDate['dates']` - the author confused this syntax with PHP's associative arrays. In JavaScript, it's the same as `markedDate.dates` and using square brackets syntax with hardcoded values makes zero sense and - for a dessert - is slower. – Robo Robok Dec 13 '21 at 09:46
  • @RoboRobok Maybe it's a dynamic field and never good to assume if not mentioned otherwise :) – Vandesh Dec 13 '21 at 09:48
  • `['dates']` is a dynamic field? Please just admit a mistake and correct it. – Robo Robok Dec 13 '21 at 09:51