-2

some text due stackoverflow error some text due stackoverflow error some text due stackoverflow error some text due stackoverflow error some text due stackoverflow error some text due stackoverflow error some text due stackoverflow error some text due stackoverflow error some text due stackoverflow errorsome text due stackoverflow error

I have associative array like this:

[Array(0)]
0: Array(0)
date_time: "Thu Apr 08 2021 12:00:00 GMT+0300 (Москва, стандартное время)"
excursionshop_currency__id: "1"
excursionshop_timetable__date_time: "2021-04-08 12:00"
excursionshop_timetable__excursionshop_excursionlanguages_id: "1"
excursionshop_timetable__id: 1
excursionshop_timetable__price: "1"
excursionshop_timetable__price_child: ""
excursionshop_timetable__price_foreigner: ""
excursionshop_timetable__price_retiree: ""
excursionshop_timetable__price_schoolchild: ""
excursionshop_timetable__price_student: ""
excursionshop_timetable__price_veteran: ""
excursionshop_timetable__seat_limit: "1"
length: 0
__proto__: Array(0)
length: 1
__proto__: Array(0)

or

[Array(0)]
0: [excursionshop_timetable__price: "1", excursionshop_timetable__price_student: "", excursionshop_timetable__price_schoolchild: "", excursionshop_timetable__price_child: "", excursionshop_timetable__price_retiree: "", …]
length: 1
__proto__: Array(0)

And i want get

[Array(0)]
0: {'excursionshop_timetable__price': "1", 'excursionshop_timetable__price_student': "", 'excursionshop_timetable__price_schoolchild': "", 'excursionshop_timetable__price_child': "", 'excursionshop_timetable__price_retiree': "", …}
length: 1
__proto__: Array(0)
MaxPodLol
  • 13
  • 3
  • 4
    Associative arrays are impossible in JS... How did you get it in the first place? Please share [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Rojo Apr 27 '21 at 15:15
  • how can i send image? – MaxPodLol Apr 27 '21 at 15:16
  • Javascript doesn't support associative Arrays, there are only numbered Arrays. – filoscoder Apr 27 '21 at 15:20
  • In JavaScript if you give named indexes instead of numbers, the array gets converted to a standard JS object. Having said that, the first format you mentioned is an array, whereas the second is JSON. Please check for JSON documentation to see how you can use in your code. – Die-Bugger Apr 27 '21 at 15:24
  • so how i got [excursionshop_timetable__price: "1"]? – MaxPodLol Apr 27 '21 at 15:24
  • @Die-Bugger "_the array gets converted to a standard JS object_" No, an array already _is_ an object. "_the first format you mentioned is an array_" No, it's a syntax error. "_whereas the second is JSON_" No, the second is an object. JSON would be the stringified version of the object (which would also add quotes around the key, as without it, it would be invalid JSON). – Ivar Apr 27 '21 at 15:28
  • Okey, all what i have if i logged this is: [Array(0)] 0: [excursionshop_timetable__price: "1", excursionshop_timetable__price_student: "", excursionshop_timetable__price_schoolchild: "", excursionshop_timetable__price_child: "", excursionshop_timetable__price_retiree: "", …] length: 1 __proto__: Array(0) – MaxPodLol Apr 27 '21 at 15:28
  • 1
    @MaxPodLol Please add clarifying information in the question, not in the comments. Also please add some context of what you are trying to achieve. – Ivar Apr 27 '21 at 15:29
  • and i need get [Array(0)] 0: {excursionshop_timetable__price: "1", excursionshop_timetable__price_student: "", excursionshop_timetable__price_schoolchild: "", excursionshop_timetable__price_child: "", excursionshop_timetable__price_retiree: "", …} – MaxPodLol Apr 27 '21 at 15:30
  • Where did you get this object from? – Thomas Apr 27 '21 at 15:37
  • 1
    Please re-word your question and problem description, it's hard to see what's going on at first glance. – Jacob Bruinsma Apr 27 '21 at 15:39
  • @Ivar, so one should not believe in w3schools (https://www.w3schools.com/js/js_arrays.asp). – Die-Bugger Apr 28 '21 at 02:07
  • @Die-Bugger I'd generally recommend [to avoid W3Schools](https://meta.stackoverflow.com/questions/280478/why-not-w3schools-com). If you are referencing the "_JavaScript will redefine the array to a standard object_" warning, then yes, that is definitely not true. The array remains an array, even when you assign values using a "named index". The element assigned will be part of the object property collection and not of the array itself and thus the `.length` wont be affected, but that doesn't mean the result is incorrect. That is just how arrays work. – Ivar Apr 28 '21 at 07:24
  • @Die-Bugger [Here is an example](https://jsfiddle.net/ck4tnmLg/). I'd always recommend to use [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#description) as documentation for JavaScript. – Ivar Apr 28 '21 at 07:24

1 Answers1

1

It appears that what you have here is actually an object already. JavaScript has no support for associative arrays. Please see my answer to a related question here.

What you are dealing with is an array with non-numeric properties, which are essentially added as properties on the object that array belongs to, rather than the array's indexes. That is why you're array has a length of zero. You can actually continue using the array as is as an object if you need to, but if you would like to remove its unnecessary array methods and use it as a plain object, we can iterate over the keys that belong to the array, and then use the array reduce() method to return all those key/value pairs in a standard JS object like this:

1. Using reduce()

const array = [];
array["date_time"] = "Thu Apr 08 2021 12:00:00 GMT+0300 (Москва, стандартное время)";
array["excursionshop_currency__id"] = "1";
array["excursionshop_timetable__date_time"] = "2021-04-08 12:00";
array["excursionshop_timetable__excursionshop_excursionlanguages_id"] = "1";
array["excursionshop_timetable__id"] = 1;
array["excursionshop_timetable__price"] = "1";
array["excursionshop_timetable__price_child"] = "";
array["excursionshop_timetable__price_foreigner"] = "";
array["excursionshop_timetable__price_retiree"] = "";
array["excursionshop_timetable__price_schoolchild"] = "";
array["excursionshop_timetable__price_student"] = "";
array["excursionshop_timetable__price_veteran"] = "";
array["excursionshop_timetable__seat_limit"] = "1";

console.log(array); // -> []

const convertArrObj2Obj = arr => Object.keys(arr).reduce((a,c) => (a[c] = arr[c], a), {});

const object = convertArrObj2Obj(array);

console.log(object);

2. Using spread syntax

Per Thomas's suggestion in the comments, you could also use the spread syntax to achieve this which is much easier. Just make sure to check browser support for spread syntax on objects.

const array = [];
array["date_time"] = "Thu Apr 08 2021 12:00:00 GMT+0300 (Москва, стандартное время)";
array["excursionshop_currency__id"] = "1";
array["excursionshop_timetable__date_time"] = "2021-04-08 12:00";
array["excursionshop_timetable__excursionshop_excursionlanguages_id"] = "1";
array["excursionshop_timetable__id"] = 1;
array["excursionshop_timetable__price"] = "1";
array["excursionshop_timetable__price_child"] = "";
array["excursionshop_timetable__price_foreigner"] = "";
array["excursionshop_timetable__price_retiree"] = "";
array["excursionshop_timetable__price_schoolchild"] = "";
array["excursionshop_timetable__price_student"] = "";
array["excursionshop_timetable__price_veteran"] = "";
array["excursionshop_timetable__seat_limit"] = "1";

console.log(array); // -> []

const convertArrObj2Obj = arr => ({...arr});

const object = convertArrObj2Obj(array);

console.log(object);
Brandon McConnell
  • 5,776
  • 1
  • 20
  • 36