Getting the value of a property - javascript

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;
}

Related

JQuery .find() function for multiple array

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()
});

splitting a string based on delimiter using javascript

Hi I'm trying to split a string based on multiple delimiters.Below is the code
var data="- This, a sample string.";
var delimiters=[" ",".","-",","];
var myArray = new Array();
for(var i=0;i<delimiters.length;i++)
{
if(myArray == ''){
myArray = data.split(delimiters[i])
}
else
{
for(var j=0;j<myArray.length;j++){
var tempArray = myArray[j].split(delimiters[i]);
if(tempArray.length != 1){
myArray.splice(j,1);
var myArray = myArray.concat(tempArray);
}
}
}
}
console.log("info","String split using delimiters is - "+ myArray);
Below is the output that i get
a,sample,string,,,,This,
The output that i should get is
This
a
sample
string
I'm stuck here dont know where i am going wrong.Any help will be much appreciated.
You could pass a regexp into data.split() as described here.
I'm not great with regexp but in this case something like this would work:
var tempArr = [];
myArray = data.split(/,|-| |\./);
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] !== "") {
tempArr.push(myArray[i]);
}
}
myArray = tempArr;
console.log(myArray);
I'm sure there's probably a way to discard empty strings from the array in the regexp without needing a loop but I don't know it - hopefully a helpful start though.
Here you go:
var data = ["- This, a sample string."];
var delimiters=[" ",".","-",","];
for (var i=0; i < delimiters.length; i++) {
var tmpArr = [];
for (var j = 0; j < data.length; j++) {
var parts = data[j].split(delimiters[i]);
for (var k = 0; k < parts.length; k++) {
if (parts[k]) {
tmpArr.push(parts[k]);
}
};
}
data = tmpArr;
}
console.log("info","String split using delimiters is - ", data);
Check for string length > 0 before doing a concat , and not != 1.
Zero length strings are getting appended to your array.

Javascript/jQuery: Loop through array values inside an object

var myJSON =
{"data":
[{"obj1":"value1",
"obj2":"value2"},
{"obj1":"value3",
"obj2":"value4"},
{"obj1":"value5",
"obj2":"value6"}]
};
I've got an array looking like one above. I'd like to loop through each obj2 and get the values. How can this be done in Javascript/jQuery?
I tried using:
for (var i = 0; i < myJSON.data.length; i++) {
console.log(i.obj2);
}
but it looks as though myJSON.data doesn't return a length...
i is only an iterator you can use to access the array
for (var i = 0; i < myJSON.data.length; i++) {
console.log(myJSON.data[i].obj2);
}
for (var i = 0; i < myJSON.data.length; i++) {
console.log(myJSON.data[i].obj2);
}
The problem is that you're trying to access the obj2 key from your i variable, which isn't your array. Try this way:
for (var i = 0; i < myJSON.data.length; i++) {
console.log(myJSON.data[i].obj2);
}
Other ways to do the same:
for(var i in d=myJSON.data){
console.log(d[i].obj2);
}
or
myJSON.data.forEach(function(a){
console.log(a.obj2)
})

Handling multidimentional array in java script is not working

function split(str)
{
var array = str.split(';');
var test[][] = new Array();
for(var i = 0; i < array.length; i++)
{
var arr = array[i].split(',');
for(var j = 0; j < arr.length; j++)
{
test[i][j]=arr[j];
}
}
}
onchange="split('1,2,3;4,5,6;7,8,9;a,b,c;d,e,f;g,h,i')"
it was not working. i need to split this string to 6*3 multi dimentional array
var array[][] = new Array() is not valid syntax for declaring arrays. Javascript arrays are one dimensional leaving you to nest them. Which means you need to insert a new array into each slot yourself before you can start appending to it.
Like this: http://jsfiddle.net/Squeegy/ShWGB/
function split(str) {
var lines = str.split(';');
var test = [];
for(var i = 0; i < lines.length; i++) {
if (typeof test[i] === 'undefined') {
test[i] = [];
}
var line = lines[i].split(',');
for(var j = 0; j < line.length; j++) {
test[i][j] = line[j];
}
}
return test;
}
console.log(split('a,b,c;d,e,f'));
var test[][] is an invalid javascript syntax.
To create a 2D array, which is an array of array, just declare your array and push arrays into it.
Something like this:
var myArr = new Array(10);
for (var i = 0; i < 10; i++) {
myArr[i] = new Array(20);
}
I'll let you apply this to your problem. Also, I don't like the name of your function, try to use something different from the standards, to avoid confusion when you read your code days or months from now.
function split(str)
{
var array = str.split(';'),
length = array.length;
for (var i = 0; i < length; i++) array[i] = array[i].split(',');
return array;
}
Here's the fiddle: http://jsfiddle.net/AbXNk/
var str='1,2,3;4,5,6;7,8,9;a,b,c;d,e,f;g,h,i';
var arr=str.split(";");
for(var i=0;i<arr.length;i++)arr[i]=arr[i].split(",");
Now arr is an array with 6 elements and each element contain array with 3 elements.
Accessing element:
alert(arr[4][2]); // letter "f" displayed

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