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('+');
});
Related
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);
why do some people store a var within another var?
For example:
var checked = $("#filterControls :checkbox:checked");
var arr = checked
A more expansive view of the code below.
var checked = $("#filterControls :checkbox:checked");
if (checked.length) {
rows.hide(200);
var arr = checked
.map(function() {
return "." + $(this).val();
})
.get();
var selector = arr.join("");
$(selector).show(200);
}
You got a wrong idea about the map function. The map function is immutable, but it returns the modified entries. Because of that you have to assign it to a new value. So checked has the unmodifed values and arr has the values which were modified by map-function.
Hi all I have problem to make some id into one array
Maybe you can help me...This image is the result
enter image description here
and this is my code
$.each(data.rows, function (i, ele) {
var tes = ele.ID;
console.log('ItemID', tes);
}
The Result that i want expect is each item in the one array like this 'ItemID = [22,2,43,2]'
const idArray = data.rows.map((r)=>r.ID)
//More verbose
const idArray = data.rows.map(function (ele){
return r.ID;
});
Map creates a loop through all the values in the array and uses the return of the function passed to create a new array.
EDIT:
I'm not sure I understand, you'd want an array of array?
const idArray = data.rows.map(function (ele){
return [r.ID];
});
var tempArray = []
$.each(data.rows, function (i, ele) {
tempArray.push(ele.ID);
}
// tempArray will be the solution
var data = [1,2,3,4]; // Assuming this array contains rows data
var resultArray = []; // Result array
var ids = data.map(function (ele){
return ele;
});
resultArray.push(ids.join(',')); // Join ids using "," separator
console.log(resultArray); // ["1,2,3,4"]
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);