saveData: function()
{
var element = $('input');
for(var i=0;i<element.length;i++)
{
//alert($(element[i]).val());
var p=new Array($(element[i]).val());
}
alert(p);
},
How to print array data in alert.
You need to create an array and then push all the values to it, instead you are resetting it in the loop
var element = $('input');
var p = element.map(function () {
return this.value
}).get();
alert(JSON.stringify(p));//or alert(p);
changing your code will be
var element = $('input');
var p = [];
for (var i = 0; i < element.length; i++) {
p.push($(element).eq(i).val());
}
alert(JSON.stringify(p));//or alert(p)
Use javascript toString() function to get result
var array = ["a","b","c"];
solution:
alert(array.toString());
In JavaScript you could just
for(i=0; i<p.length; ++i){
alert(p[i]);
}
so if p is an array containing ["one","two","three"] your browser will alert "one", "two" and then "three" in the loop.
To display array values properly try this :
saveData: function()
{
var p=new Array();
var element = $('input');
for(var i=0;i<element.length;i++)
{
//alert($(element[i]).val());
p[0] = $(element[i]).val();
}
alert(p.join("\n"));
},
Use javascript forEach() function to get result
var array = ["a","b","c"];
solution:
array.forEach(function(data){alert(data)});
You can use join().
It converts each array element into a string.
It acts much like toString(), moreover you can determine the separator.
var arr = ["HTML","CSS","JS"];
...
alert(arr.join(' - '));
Related
var filterKeys = ["hello this is javascript", "Movies"];
var title= "Hello"
searchKeyInNo = keySearch(filterKeys, title)
function KeySearch(filteredArray, searchKey) {
var flag=0
for (var i = 0; i<filteredArray.count; i++) {
//array operations
flag=1
}
return flag
}
var filterKeys = ["hello this is javascript", "Movies"];
var title= "Hello"
searchKeyInNo = keySearch(filterKeys, title)
function KeySearch(filteredArray, searchKey) {
var tempArray = filteredArray
var flag=0
for (var i = 0; i<tempArray.count; i++) {
//array operations
flag=1
}
return flag
}
Can we use passed array as paramneter as a function array?
Can we pass and use the entire array as argument?
I even tried the second piece of code that I added a new array to copy the argument array to process the function scoped array.
UPDATE: VS Code never showed up suggestion for argumentarray.length. Thanks. WORKS NOW!
Try this. You were using .count instead of .length on the filtered array to get the count of elements.
function KeySearch(filteredArray, searchKey)
{
var flag=0
for (var i = 0; i<filteredArray.length; i++)
{
if(filteredArray[i].toLowerCase()
.includes(searchKey.toLowerCase()))
{
flag++
}
}
return flag
};
console.log(KeySearch(["hello this is javascript", "Movies"], "Hello"))
I don't know what must be title for my question, I think it's so complicated. So, I have A array:
["87080207", "87101133", "91140156"]
And B Array:
["97150575", "97150575", "90141063"]
This B array, I put on html select value. Each of them(A and B array) is related. I need to show 87080207,87101133 (A array) when I choose value 97150575 (B array).
I have tried, but it didn't work.This is my code:
var a=[];
var b=[];
var arrayLength = dataComponentValuation.length;
for (var i = 0; i < arrayLength; i++) {
a.push(dataComponentValuation[i].valuated);
b.push(dataComponentValuation[i].valuator);
}
var ajoin = a.join();
var bjoin = b.join();
$('#valuatedEmpCompId_before').val(ajoin);
$('#valuator_before').val(bjoin);
In select, I put a function, this is it:
function emptyValuated() {
var valby = $("#valBy").val(); //chosen value from select
var b_valby = $("#valuator_before").val();
var b_valuated = $("#valuatedEmpCompId_before").val();
if(b_valby != ''){
if(valby != b_valby)
{
$("#valuatedEmpCompId").val('');
}
else{
$("#valuatedEmpCompId").val(b_valuated);
}
}
else{
$("#valuator_before").val(valby);
$("#valuatedEmpCompId").val(b_valuated);
}
}
Help me please...
As suggested, you could use an object as reference to the values of array A.
var arrayA = ["87080207", "87101133", "91140156"],
arrayB = ["97150575", "97150575", "90141063"],
object = Object.create(null);
arrayB.forEach(function (b, i) {
object[b] = object[b] || [];
object[b].push(arrayA[i]);
});
console.log(object);
I guess nowadays the Map object is a perfect solution for these jobs.
var arrayA = ["87080207", "87101133", "91140156"],
arrayB = ["97150575", "97150575", "90141063"],
myMap = arrayB.reduce((p,c,i) => p.has(c) ? p.set(c, p.get(c).concat(arrayA[i]))
: p.set(c,[arrayA[i]])
, new Map());
console.log(myMap.get("97150575"));
console.log(myMap.get("90141063"));
i have an array like
myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.]
How can i retrive an array with only id like?
myArrayResult = [73,45]
with jQuery or javascript.
thanks
Use map
myArrayResult = myArray.map(function (el) {
return el.id; // if you want get id as Number, just add +el.id
})
Example
You could do
var myArrayResult = myArray.map(function (item) {
return item.id;
}
Or if you don't have a true browser (ie. IE8 or less)
var myArrayResult = [];
for (var i = 0, l = myArray.length; i < l; i++) {
myArrayResult.push(myArray[i].id);
}
myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}];
myArrayResult=[];
$.each(myArray,function(index,val){
myArrayResult.push(val.id);
});
alert(myArrayResult);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
I have two arrays
var array1 = ['me','you','our'];
var array2 = ['us','they','all'];
I have another array
var arrayList = [array1, array2]
Now I have one value which I want to compare with each value of each array inside arrayList.
How can we do that?
Try this...
var yourValue;
for(var i=0;i<arrayList.length;i++)
{
for(var j=0;j<arrayList[i].length;j++)
{
if(arrayList[i][j] == yourValue)
{
//
//
}
}
}
var val='your value';
for(var i=0;i<arrayList.length;i++)
{
if(arrayList[i].indexOf(val)>-1){
// do something
// and break
}
}
Loop through arrayList then use indexOf.
var val = 'you';
for(var i = 0; i < arrayList.length; i++){
if(arrayList[i].indexOf(val) !== -1){
alert('match');
}
}
If I have the following string:
mickey mouse WITH friend:goofy WITH pet:pluto
What is the best way in javascript to take that string and extract out all the "key:value" pairs into some object variable? The colon is the separator. Though I may or may not be able to guarantee the WITH will be there.
var array = str.match(/\w+\:\w+/g);
Then split each item in array using ":", to get the key value pairs.
Here is the code:
function getObject(str) {
var ar = str.match(/\w+\:\w+/g);
var outObj = {};
for (var i=0; i < ar.length; i++) {
var item = ar[i];
var s = item.split(":");
outObj[s[0]] = s[1];
}
return outObj;
}
myString.split(/\s+/).reduce(function(map, str) {
var parts = str.split(":");
if (parts.length > 1)
map[parts.shift()] = parts.join(":");
return map;
}, {});
Maybe something like
"mickey WITH friend:goofy WITH pet:pluto".split(":")
it will return the array, then Looping over the array.
The string pattern has to be consistent in one or the other way atleast.
Use split function of javascript and split by the word that occurs in common(our say space Atleast)
Then you need to split each of those by using : as key, and get the required values into an object.
Hope that's what you were long for.
You can do it this way for example:
var myString = "mickey WITH friend:goofy WITH pet:pluto";
function someName(str, separator) {
var arr = str.split(" "),
arr2 = [],
obj = {};
for(var i = 0, ilen = arr.length; i < ilen; i++) {
if ( arr[i].indexOf(separator) !== -1 ) {
arr2 = arr[i].split(separator);
obj[arr2[0]] = arr2[1];
}
}
return obj;
}
var x = someName(myString, ":");
console.log(x);