Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does js EVAL function change position of elements?

Tags:

javascript

php

I have an application in PHP and JS. When I EVAL the json encoded PHP array the array sort changes. For example, if I have an array in PHP like this:

<?php 
$array = [148 => 'Plane', 149 => 'Car'];
?>

<script>
var array = eval(<?php echo json_encode($array)?>);
</script>

When I print the array in console, the elements doesn't have the same position. Do you know how can this happens?

UPDATE

Thanks for the answers but I want to keep the exactly same order in a JS structure, so I don't want to order the array by a specific field. Maybe the order get from the DB is like:

[148 => object, 155 => object, 133 => object]

I want to create an array like this in JS with the order that it has (the position come from DB and it has to be that order). Is it possible?

like image 560
Javier Núñez Avatar asked Jan 26 '16 12:01

Javier Núñez


People also ask

Why is it bad to use eval in JavaScript?

Malicious code : invoking eval can crash a computer. For example: if you use eval server-side and a mischievous user decides to use an infinite loop as their username. Terribly slow : the JavaScript language is designed to use the full gamut of JavaScript types (numbers, functions, objects, etc)… Not just strings!

What does JavaScript eval function do?

eval() is a function property of the global object. The argument of the eval() function is a string. It will evaluate the source string as a script body, which means both statements and expressions are allowed. It returns the completion value of the code.

Is eval asynchronous?

eval is synchronous in nature. But the evaluations/expressions inside the eval may have asynchronous code like setTimeout or setInterval .

Which is faster eval () or new function?

`Function() is a faster and more secure alternative to eval().


2 Answers

<?php echo json_encode($array)?>

Since the array is sparse, this resolves to

{"148":"Plane","149":"Car"}

which is an object and object property order is not guaranteed in JS.

http://php.net/manual/en/function.json-encode.php

Note: When encoding an array, if the keys are not a continuous numeric sequence starting from 0, all keys are encoded as strings, and specified explicitly for each key-value pair.

You can solve this by creating an array from the object, like this:

var obj = <?php echo json_encode($array)?>; // note, eval not needed
var arr = []; 
Object.keys(obj).forEach(function(key) { 
    arr[key] = obj[key]; 
});

Concerning the update:

You need to save the order of the keys separately.

var order = <?php echo json_encode(array_keys($array))?>;
var obj = <?php echo json_encode($array)?>;
order.forEach(function(key) {
    console.log(key, obj[key]); // or whatever you need
});

You can even construct an ordered map (which PHP's arrays actually are, unlike the arrays in JS) if you use ES6 or a polyfill.

like image 161
ekuusela Avatar answered Oct 01 '22 00:10

ekuusela


The earlier posters have already answered the question. Just to add to it:

Many people get confused because they think of Javascript Objects as associative arrays in PHP. However that is quite not the case. While its true that we can (sort of) simulate a data structure close to a PHP associative array by using objects in Javascript, they are totally different data structures and do not work quite the same way as arrays do.

In arrays the integrity of index position is important from a data structure and index relation perspective, which is why their order is maintained. However the same rule does not matter to objects since their "pseudo-named-index" (which really is just the property name), is not place-dependent. It can exist in any order as long as that property still has the same value assigned to it.

Hope this helps.

like image 37
raidenace Avatar answered Sep 30 '22 23:09

raidenace