In my current scenario, I would like to remove the level of index numbers from an array. Since the keys are not fixed and may change from time to time, it will affect my script in the long run.
I have found code that does the job using PHP. But how can I do it in JS?
PHP
$newArray = array();
foreach($value as $val) {
$newArray = array_merge($new, $val);
}
JS
var newArray = [];
oldArray.forEach(function(entry){
newArray = newArray.concat(entry);
});
PHP SAMPLE DATA AND RESULT
array(3) {
[0]=>
array(1) {
["name"]=>
string(8) "John Doe"
}
[1]=>
array(1) {
["age"]=>
string(2) "24"
}
[2]=>
array(1) {
["sex"]=>
string(4) "male"
}
}
TO THIS
array(3) {
["name"]=>
string(8) "John Doe"
["age"]=>
string(2) "24"
["sex"]=>
string(4) "male"
}
Above samples are from PHP output which I also want to attain in JS.
With my code in js
, it is not removing the level of indexes of the array. I want to remove the indexes of the array so that I get a plain object in which I can access its data like newArray['name']
without passing through an index since its arrangement is unpredictable.
Please let me know if you have clarifications for I really need your help on this.