I have a dictionary like this :
var iso_map = {'AFG':'AF','ALB':'AL','DZA':'DZ','ASM':'AS','AND':'AD','AGO':'AO','AIA':'AI','ATA':'AQ','ATG':'AG'}
I have an array like this :
var isoCodes = ['AFG','AFG','AFG','AGO,'AGO','AFG','AFG','AND','AGO']
I want to replace the above array to give me an output like this:
var isoCodes - ['AF','AF','AF','AO,'AO','AF','AF','AD','AO']
Just use map
var iso_map = {'AFG':'AF','ALB':'AL','DZA':'DZ','ASM':'AS','AND':'AD','AGO':'AO','AIA':'AI','ATA':'AQ','ATG':'AG'};
var isoCodes = ['AFG','AFG','AFG','AGO','AGO','AFG','AFG','AND','AGO']
var result = isoCodes.map(x => iso_map[x]);
console.log(result);
Related
I'm trying to convert url. Using for loop to extract Acura, Audi. Here what I got so far:
var newSrpParams = 'year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';
const newSrpParamsArray = newSrpParams.split("&");
var oldSrpParams;
var makes = [];
for(var i = 0 ; i < newSrpParamsArray.length; i++){
if(newSrpParamsArray[i].includes('make')) {
const make = newSrpParamsArray[i].replace('make=','')
makes.push(make);
console.log(makes)
}
};
The result is
[ 'Acura' ]
[ 'Acura', 'Audi' ]
As you see it has one more array. Is there a way to get only [ 'Acura', 'Audi' ]?
FYI there's a native solution for getting values a from query string, check URLSearchParams
var newSrpParams = 'year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';
const makes = new URLSearchParams(newSrpParams).getAll('make');
console.log(makes);
That is happening because you are logging the array inside the for loop. If you move it outside you will get
['Acura', 'Audi']
The Code:
var newSrpParams = 'year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';
const newSrpParamsArray = newSrpParams.split("&");
console.log(newSrpParamsArray)
var oldSrpParams;
var makes = [];
for(var i = 0 ; i < newSrpParamsArray.length; i++){
if(newSrpParamsArray[i].includes('make')) {
const make = newSrpParamsArray[i].replace('make=','')
console.log(make)
makes.push(make);
}
};
console.log(makes) // The change
You were consoling the results inside the if statement it will run two times. So as a result make[] array print two times. That's why you get the two arrays.
var newSrpParams = 'year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000';
const newSrpParamsArray = newSrpParams.split("&");
var oldSrpParams;
var makes = [];
for(var i = 0 ; i < newSrpParamsArray.length; i++){
if(newSrpParamsArray[i].includes('make')) {
const make = newSrpParamsArray[i].replace('make=','')
makes.push(make);
}
};
console.log(makes)
Make sure to console make[] from outside of the for a loop. That's only. I couldn't see any other wrong line in your code.
Why not use URLSearchParams?
and you can replace the URL with window.location.href
let url = new URL(`http://localhost?year=2020-2022&make=Acura&make=Audi&model=A3&model=A5&trim=2.0T%20Premium&trim=2.0T%20S%20line%20Premium&normalBodyStyle=Hatchback&normalBodyStyle=Sedan&odometer=13000-38000&internetPrice=20000-50000`)
let params = new URLSearchParams(url.search).getAll("make")
console.log(params)
Currently I have a PHP page returning some values. The data is something like this:
08-30-2018, in
08-29-2018, out
08-28-2018, in
08-27-2018, in
How can I create a custom array in Javascript with the values above to be similar as this array below:
var system = [
['08-30-2018', 'in'],
['08-29-2018', 'out'],
['08-28-2018', 'in'],
['08-27-2018', 'in']
];
I have tried array.push, but it does not create an array like above.
What should I do? Can you help me? Thank you!
You can use multi-dimensional arrays in JavaScript
var system = [];
var output = "08-30-2018, in\n08-29-2018, out\n08-28-2018, in\n08-27-2018, in";
var items = output.split("\n");
for(i=0; i<items.length; i++){
var data = items[i].split(",");
var item = [];
item.push(data[0].trim());
item.push(data[1].trim());
system.push(item);
}
console.log(system);
You could also parse this kind of input using regular expressions:
const input = '08-30-2018, in\n08-29-2018, out\n08-28-2018, in\n08-27-2018, in';
const regex = /(\d{2}-\d{2}-\d{4}), (in|out)/g;
let system = [];
let match;
while ((match = regex.exec(input)) !== null) {
system.push([match[1], match[2]]);
}
I have a function:
chatManager.connect()
.then(currentUser => {
var final = currentUser.users;
var name = [];
var arr = [];
$.each(final,function(index,val){
if(val.name != '{{Auth::user()->name}}')
{
console.log(val.id); //Harry, Ron (each in different line)
arr = name.push(val.id)
console.log(arr); //1,2 (each in different line)
var presence = val.presenceStore.store.{{Auth::user()->name}}{{Auth::user()->id}}
}
});
I want the arr to be an array like [Harry,Ron]. Why is it not working? I am new to Jquery. Please help.
arr = name.push(val.id) is your problem. push returns the array's new length, not an array. Simply replace the line with
arr.push(val.id);
For example I have brunch of data like below:
HTML:
<p class="test-tag">abc+dd</p><p class="test-tag">gf+sx</p>
and store in JavaScript in array form
var text = ["abc+dd","gf+sx"];
And I must return an array like below:
var res = [["abc", "dd"],["gf", "sx"]];
What's the best way to do this?
Something like below should work..
var finalArr = [];
$('.test-tag').each (function (){
var value = $(this).text;
var subArr = value. map(function (b){
return b.split('+');
})
finalArr. push(subArr);
})
You can map over your array and use split:
Example JSBin
var text = ["abc+dd","gf+sx"];
var array = text.map(function(v) {
return v.split('+');
});
I have this function:
var chart = d3.parsets()
.dimensions(["Survived", "Age", "Class"]);
I want to replace ["Survived", "Age", "Class"] with a string from an other variable. So i can change it. In this example: .dimensions(["Survived", "Age"]);
What i have is:
var strDimensions = ('"Survived", "Age"');
var chart = d3.parsets()
.dimensions([strDimensions]);
but that doesn't work
var strDimensions = ('"Survived", "Age"');
var chart = d3.parsets()
.dimensions([strDimensions]);
What you are getting is an array with one index, not two.
If you want to use the string, you can split it to make it into an array.
var strDimensions = ('Survived,Age');
var chart = d3.parsets()
.dimensions(strDimensions.split(",");
Why don't you just start off with the array to begin with?
Your array syntax is wrong, try this instead:
var strDimensions = ['Survived', 'Age'];
var chart = d3.parsets().dimensions(strDimensions);
what you need is
var dims = [];
now you can add and remove from the array as you wish
and then use
var chart = d3.parsets().dimensions(dims);