Reputation: 135
I'm trying to fix my view to where the database has the naming conventions of the rows in the wrong format. I can't correct these conventions within the query itself as it would affect a lot of functionality within the website. Therefore, I decided to rename them correctly with JavaScript so they display with the right naming convention in the view.
To do this, I had to write my query where it returns a fictional array like so:
Original Array
['Tree_Domestic', Rabbit, Unicorn, Cheetah_Domestic, Shark, Whale_Domestic]
What I want is to scan the entire array and only find entries that do not have "_domestic" or "_international" and replace them with "_international". For example, [Rabbit,Unicorn,Shark
] do not have _domestic nor do they have _international so I want them to be like so:[Rabbit_International,Unicorn_International,Shark_International]
I managed to do this successfully but I ran into the last issue,
It modified the order of the array in alphabetical order and I don't want that. I want the array to look like this:
['Tree_Domestic', Rabbit_International, Unicorn_International, Cheetah_Domestic, Shark_International, Whale_Domestic]
The reason I need it to look like this is because in my query, I'm also counting the most popular rows and if I modified the array with my count, the count won't go into the correct order with the array items that were sorted with the modified array.
Here is my query:
$sql = 'SELECT animals,
COUNT(*)
FROM fictional_signup
WHERE usergroup NOT IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
GROUP BY popular
ORDER BY COUNT(*) DESC';
Javascript
var renameLocations = [];
dataLabel = <?php echo json_encode($locations) ?>;
dataCount = <?php echo json_encode($count) ?>;
for(t = 0; t < dataLabel.length; t++){
if(dataLabel[t].includes('_Domestic') || dataLabel[t].includes('_International')){
renameLocations.push(dataLabel[t]);
}
}
for(z = 0; z < dataLabel.length; z++){
if(!dataLabel[z].includes('_Domestic') && !dataLabel[z].includes('_International')){
renameLocations.push(dataLabel[z] + "_International");
}
}
// Returns the correct naming conventions but the order is incorrect with the count.
console.log(renameLocations);
Upvotes: 0
Views: 217
Reputation: 25820
You can use the Array.prototype.map()
function to create a new array with modified entries.
/*
$locations = [
'Tree_Domestic',
'Rabbit',
'Unicorn',
'Cheetah_Domestic',
'Shark',
'Whale_Domestic'
]
*/
dataLabel = <?php echo json_encode($locations) ?>.map(
// "e" represents each entry in the array, one at a time
function(e){
// if the entry ends with _Domestic or _International,
// then just keep the value
if (e.endsWith('_Domestic') || e.endsWith('_International'))
return e;
// otherwise, append "_International" to the entry and use that
else
return e + "_International";
}
)
Produces:
[
"Tree_Domestic",
"Rabbit_International",
"Unicorn_International",
"Cheetah_Domestic",
"Shark_International",
"Whale_Domestic"
]
Upvotes: 1
Reputation: 24965
You can map the original array into a new array. Doing so will keep the elements in their original order.
var originalArray = ['Tree_Domestic', 'Rabbit', 'Unicorn', 'Cheetah_Domestic', 'Shark', 'Whale_Domestic'];
var modifiedArray;
modifiedArray = originalArray.map(function(value, index){
if (value.indexOf('_Domestic') < 0 && value.indexOf('_International') < 0) {
return value +'_International';
}
return value;
});
console.log(modifiedArray);
Upvotes: 0