JQuery .find() function for multiple array - javascript

Here is my code snippet
for (var k = 0; k < link_list.length; k++) {
var service_list = document.getElementsByName("service_info");
service_list = $(service_list).children("div[name=service_info_element]");
for (var i = 0; i < service_list.length; i++){
var service_info = {};
service_info["service_name"] = $(service_list[i]).find("select[name=service_name]").val();
service_info["service_type"] = $(service_list[i]).find("input[name=service_type]").val();
}
How can I get $(service_list[i]).find("select[name=service_name]").val() and $(service_list[i]).find("input[name=service_type]").val(); for each link_list[k] inside the second loop. I mean I need something like link_list[k].service_list[i].find("select[name=service_name]").val()

You can try this it'll work
$('service_list[i]').find('select[name=service_name]').filter([0,3,4]).anything();

Try the following:
link_list[k].service_list[i].find("select[name=service_name]").each(function(i, element){
var val = $(this).val(); // or element.val()
});

Related

How to obtain stock ticker symbol from this data in JavaScript?

I wanted to obtain the stock ticker symbol from this data. This is the progress I made so far. Afterwards, I just ended up going in circles. I was trying to get the ticker characters together in an array like this: ['VOO', 'DIS'...]
var data = "VOO2.1315$232.86$23.34$496.34DIS1.94733$96.40$12.09$187.72IWM1.07511$112.56$4.24$121.01DAL5$29.55-$16.70$147.75XOM2$36.95-$29.42$73.90VYM1$69.75-$0.69$69.75SBUX1.19403$66.34$2.82$79.21HAL11$6.39$1.21$70.29SFYX9$7.61-$0.40$68.45O1$56.90$2.07$56.90WPG40$1.03-$44.80$41.20SFY6$9.06$0.00$54.36SCHA1$50.96$2.22$50.96DIG10$4.58-$5.40$45.80MO1$36.64-$2.73$36.64DVN6$6.29-$4.26$37.74CSCO1$38.82$5.99$38.82AEG14$2.62-$17.64$36.68XLE1$28.33-$1.91$28.33DOW1$28.56$5.31$28.56RDSA1$31.64$5.46$31.64CBL50$0.24-$13.86$12.14";
function tickerSep(info){
var result = [];
for (var i = 0; i < info.length; i++){
if (info[i].match(/([A-Z])/g)) {
result.push(info[i]);
}
}
console.log(result);
}
tickerSep(data);
The regex g flag will do that for you.
function tickerSep(info) {
return info.match(/[A-Z]+/g);
}
Read more about it here.
this should do the trick. Let me know if you don't understand.
var data = "VOO2.1315$232.86$23.34$496.34DIS1.94733$96.40$12.09$187.72IWM1.07511$112.56$4.24$121.01DAL5$29.55-$16.70$147.75XOM2$36.95-$29.42$73.90VYM1$69.75-$0.69$69.75SBUX1.19403$66.34$2.82$79.21HAL11$6.39$1.21$70.29SFYX9$7.61-$0.40$68.45O1$56.90$2.07$56.90WPG40$1.03-$44.80$41.20SFY6$9.06$0.00$54.36SCHA1$50.96$2.22$50.96DIG10$4.58-$5.40$45.80MO1$36.64-$2.73$36.64DVN6$6.29-$4.26$37.74CSCO1$38.82$5.99$38.82AEG14$2.62-$17.64$36.68XLE1$28.33-$1.91$28.33DOW1$28.56$5.31$28.56RDSA1$31.64$5.46$31.64CBL50$0.24-$13.86$12.14";
function tickerSep(info){
var result = [];
var temp = '';
var myArray = data.split('$');
var len = myArray.length;
for (var i = 0; i < len; i++){
temp ='';
for(var j = 0;j<myArray[i].length;j++){
if (myArray[i][j].match(/([A-Z])/g)) {
temp =temp + myArray[i][j];
}
}
if(temp != '')result.push(temp);
}
console.log(result);
}
tickerSep(data);
function tickerSep1(info) {
return info.match(/[A-Z]+/g);
}
console.log(tickerSep1(data));

Getting the value of a property

How do I get the value of BrandID?
Use this yourObjectArray[0].Item.BrandID
For your example:
objRef[0]["Item"]["BrandID"]
or you can use:
objRef[0].Item.BrandID
For Iterating in your array you will use:
var i;
for(i=0; i < objRef.Length ; i++)
{
var brandId = objRef[i].Item.BrandID;
// or
var brandId = objRef[i]["Item"]["BrandID"];
// your code blocks to process blockId
}
Go with this ..
ObjectArray[0].Item.BrandID
You mean this?
for(var i = 0, len = yourObjectArray.length; i < len; i++) {
var youNeed = yourObjectArray[i].Item.BrandID;
}

How to make array with string list

I have a generated list which is like this
196-1526, 85-651, 197-1519
I need the array like this. Each node has two part. I need only the first part of each node in one array.
196, 85, 197
I already have this code which generage 196
str.substr(0,str.indexOf('-'));
You could use the following:
'196-1526, 85-651, 197-1519'.replace(/-\d+(,|$)/g, '').split(/\s/)
If the input is a string you can use split() and push(), similar to this:
var x = "196-1526, 85-651, 197-1519"
var y = x.split(',');
var myArray = [];
for(i = 0; i < y.length; i++){
myArray.push(y[i].split('-')[0].trim());
}
DEMO - Using split() and push()
if it's an array
var myarray = ["196-1526", "85-651", "197-1519"];
var newarray = [];
var i = 0;
for(i = 0; i < myarray.length; i++){
var mnode = myarray[i].split("-");
newarray.push(mnode[0].trim());
}
and if it's a string
var myarray = "196-1526, 85-651, 197-1519".split(",");
var newarray = [];
var i = 0;
for(i = 0; i < myarray.length; i++){
var mnode = myarray[i].split("-");
newarray.push(mnode[0].trim());
}
Demo: http://jsfiddle.net/Dbbc8/
try this code using split
var text='196-1526, 85-651, 197-1519';
var splittedtext=text.split(',');
var numbers=new Array();
for(var i=0;i<splittedtext.length;i++)
{
var furthsplit=splittedtext[i].split('-');
numbers[i]=furthsplit[0];
}
alert(numbers);
var pairs = str.split(", ");
var values = [];
for (var i=0; i< pairs.length; i++) {
values.push(pairs[i].substr(0, pairs[i].indexOf('-')));
}

jQuery dynamically increment variable name inside a for-loop

is it possible to add i to a var inside a for-loop?
in wrong syntax it would look like the code below
for(i=1; i<=countProjects; i++){
var test + i = $(otherVar).something();
};
Thanks!
It would be best to use an array for this:
var test = [];
for (i = 1; i <= countProjects; i++) {
test[i] = $(otherVar).something();
};
Then you could access the values like this:
console.log(test[1]);
console.log(test[2]);
etc...
If you have really good reason to have named variables for each value, you can create them like this:
for (i = 1; i <= countProjects; i++) {
window["test" + i] = $(otherVar).something();
};
console.log(test1);
As Mat stated, you should be using arrays for this type of functionality:
var projects = [];
for (var i = 0; i <= countProjects; i++) {
projects.push($(otherVar).something());
}
You could craft variable names, using object["varname"] syntax. But it's _generally_ bad practice:
var varName;
for (var i = 0; i <= countProjects; i++) {
varName = "test" + i.toString();
this[varName] = $(otherVar).something();
}
console.log(test1);

Remove <param> node from <object> and replace it with new <param> values

How can I add tag to Object , replacing its original tag with modified values. I am using the JavaScript to perform this replacement.
But the param is not getting replaced or removed.
how to identify a particular param under
Can any one suggest please.
Thank you!
function autoObjectFun() {
var objects = document.getElementsByTagName('object');
var len1 = objects.length;
for(var j = 0; j < len1; j++) {
var paramObj = objects[j].getElementsByTagName('param');
var len = paramObj.length;
for(var i = 0; i < len; i++) { var srcStr = paramObj[i].getAttribute('name'); if (srcStr == 'flashvars' ) { var newParamObj = document.createElement('param'); newParamObj = paramObj[i].cloneNode(true); var params = paramObj[i].getAttribute('value'); var newparams = ''; var paramplay = 'autoplay=0&'; newParamObj.setAttribute('value', paramplay);
paramObj[i].removeNode(true);
var newObject = objects[j].cloneNode(true);
var parent1 = objects[j].parentNode;
newObject.appendChild(newParamObj);
parent1.replaceChild(newObject,objects[j]); }
}}}
There is no native method removeNode(), this will cause an error and stop the further execution of your statements.
Try:
paramObj[i].parentNode.removeChild(paramObj[i]);
..instead.

Categories

Resources