Creating array with multiple objects Javascript from XML data - javascript

I have a problem approaching this problem and I'm unsure in how to solve it. My XML looks like this:
<Order>
<OrderLines>
<ProductLine>
<ManufacturerArticleNo>ARTICLENUMBER</ManufacturerArticleNo>
<Serials>
<SerialNumber>
.
.
<SerialNumber>
</Serials>
<ProductLine>
<ManufacturerArticleNo>ARTICLENUMBER</ManufacturerArticleNo>
<Serials>
<SerialNumber>
.
.
<SerialNumber>
</Serials>
What I want to do is create an array, and fill it with objects with articleNumber, serialnumbers. Where I'm stuck is getting all the serialNumbers from a node to the corresponding article number.
Edits after comments:
Expected output: An array filled with the objects with ArticleNumber, Corresponding serial number. One serial number = one object.
Current Code:
//Create Article List
var artList = [];
//Get articles
var nodeArticleNo = xmlDoc.getFirstNode('//ProductLine/ManufacturerArticleNo');
while(nodeArticleNo != null) {
var artNo = xmlDoc.getNodeText('//ProductLine/ManufacturerArticleNo');
//Get Serials
var nodeSerialNumber = xmlDoc.getFirstNode('//Serials/SerialNumber');
while(nodeSerialNumber != null){
var serialNo = xmlDoc.getNodeText('//Serials/SerialNumber');
artList.push({ArticleNumber: artNo, SerialNumber, serialNo});
nodeSerialNumber = xmlDoc.getNextNode(nodeSerialNumber);
}
nodeArticleNo = xmlDoc.getNextNode(nodeArticleNo);
}
This is how I have been trying to solve it but I hate the while in a while and it is not working as intended
Thank you!

Related

Search a large array in Javascript more efficiently?

I am writing a script that allows organisation of records and data.
However a problem right now is I use an array to store a large amount of information and at a point in my script this array is searched for a matching id and updated. The data search begins to get quite slow when the array size is more than 200 elements.
Is there any way to more efficiently search the array?
I am currently using find.(function(a){return x == y};);
Each element in my array is also a tuple.
Sample code:
onPipelineChanged: function(component, event, helper)
{
var titleid = event.getParam("titleid");
var title = event.getParam("title");
var maxSeats = event.getParam("maxSeats");
var seatsInUse = event.getParam("seatsInUse");
var item = event.getParam("item"); //This is the item that was moved.
var allGuestsList = component.get("v.allItems");
//Searches guest list for matching guest of who got moved.
var actualGuest = allGuestsList.find(function(moving){return moving.id == item.id;}); //***REALLY INEFFICIENT***

count the number of rows in 2d array

Description:
I am adding the data in my 2D array like the following
my_2d_array['user1'] = {'id':name,'socket':socket};
my_2d_array['user2'] = {'id':name,'socket':socket};
This 2D array keeps the record of all the connected users id and their respective sockets ...
I want to display the number of users connected .. So far to do that would be to count the number of rows in the array and display it
I have tried following:
my_2d_array[].length; // this gives nothing
my_2d_array.length; // this outputs 0 (as number)
What should I do to get the number of rows
UPDATE
I declared my array like this
var my_2d_array = [];
This could work for you
// initialize
var my_2d_array = {};
// add users
my_2d_array["user1"] = ...
my_2d_array["user2"] = ...
// get number of users
Object.keys(my_2d_array).length;
//=> 2
You should consider using users instead of my_2d_array though. It communicates better and the actual data type is an Object, not specifically an Array.
Use push method
my_2d_array.push({'id':name,'socket':socket});
my_2d_array.push({'id':name,'socket':socket});
And my_2d_array.length to get the count
It looks like you are trying to figure out how many keys are in your javascript object my_2d_array.
You should be able to use Object.keys()
Here is a JsFiddle.
var my_2d_array = {};
var name = "Hello";
var socket = "World";
my_2d_array['user1'] = {'id':name,'socket':socket};
var name = "Hello2";
var socket = "World2";
my_2d_array['user2'] = {'id':name,'socket':socket};
alert( Object.keys(my_2d_array).length );

minimized code for arrays in js

I have three arrays in js and now i want to add empty check on them..so please help me in short/ minimized code for the empty array check.My js code is
var selectedfirst = jQuery('select#frsts').val();
var selectedsecond = jQuery('select#secnds').val();
var selectedthird = jQuery('select#thirds').val();
var lastfindal = selectedfirst.concat(selectedsecond); // Array concatination
var getfinal = lastfindal.concat(selectedthird); // Array concatination
I know how can i process empty check on single array but due to contcatenation the code goes longer . i contcate first array to second then concate to third.I want to concate array when they are not empty. like selectedfirst.length > 0.if anyone not understand fully i will provide more detail on request.Thanks in advance
After working i fix the issue.I created new array like
var hege = [];
if (selectedfirst!== null) {
alert('not emtpy');
var hege = hege.concat(selectedfirst);
}
the same condition for other too.

Filling arrangement from a database

I need to fill an array with each of the values ​​for "name" and "nickname" taken from a SQLITE database and show it in an alert.
This is for a project written in JavaScript in titanium appcelerator.
This is the code:
var fightersRS = db.execute('SELECT * FROM fighters');
var setFighters = [];
while (fightersRS.isValidRow())
{
var name = fightersRS.fieldByName('name');
var nickname = fightersRS.fieldByName('nickname');
setFighters.push = {
name: name,
nickname: nickname
};
fightersRS.next();
}
alert(setFighters);
The idea is that the alert displays all comma separated values, for example:
"muhammad ali, rocky balboa, ten shin han, etc ...."
How I can do it? my code does not work.
Change your code as follows
var fightersRS = db.execute('SELECT * FROM fighters');
var setFighters = [];
while (fightersRS.isValidRow())
{
var name = fightersRS.fieldByName('name');
var nickname = fightersRS.fieldByName('nickname');
setFighters.push({"name": name, "nickname": nickname}); //This is the correct syntax for pushing an element to the array
fightersRS.next();
}
You can display the elements using a for loop as follows
for(var i=0; i <setFighters.length;i++){
alert("Name = " + setFighters[index].name + " Nickname= " + setFighters[index].nickname);
}
If you want to display their name with comma, try the following
var fightersRS = db.execute('SELECT * FROM fighters');
var setFighters = [];
while (fightersRS.isValidRow())
{
var name = fightersRS.fieldByName('name');
setFighters.push(name);
fightersRS.next();
}
var fighters = setFighters.join(",");//Joining the array elements with comma
alert(fighters); //Will display all names with a comma
Look at javascript join method
Use a for loop or inside the while loop only create a string appending all the values with comma.
Fetching all the data from an array with comma is not possible.
Here's an asccociative array example from stack over flow .. you can have your own for loop or use the while loop you are creating
Associative array and fetching data
Thanks

Add values to end of multidimensional array

I have an array of arrays and I want to be able to add values to the array every time a user hits the add button.
var transactions = [];
transactions[0] = []; // holds date
transactions[1] = []; // holds transaction type
transactions[2] = []; // holds amount
transactions[0][i] = $("date").value;
transactions[1][i] = $("transType").value;
transactions[2][i] = parseFloat( $("amount").value);
these are the snippets . . . I realize I need to work on validation (which I will do) at this point I'm more concerned with loading the arrays. The problem is the method I'm familiar with works with 1-dimensional arrays and I don't know how to replace it.
in short is it possible to reproduce this:
transactions[transactions.length] = $("date").value;
for a multidimensional array.
Thank You
Use push:
transactions[0].push( $("date").value );
You're looking for the Array.push() method. It allows you to push values on to the end of an array.
For your use case, you can do that on both levels of the array. See below:
var transactions = [];
transactions.push([]);
transactions.push([]);
transactions[0].push( $('date').value );
There is no problem for using the same approach:
transactions[0][transactions[0].length] = $("date").value;
or with push method:
transactions[0].push($("date").value);
... keeping in mind that you've initialized transactions[0] with empty array [].
Yes, just idem for multidimensional
transactions[0][transactions[0].length] = var1;
Seems a little weird you are doing:
[[array of transaction dates],[array of transaction types],[array of transaction amounts]]
Are you sure you don't want
[array of transactions, with date,type,amount properties]
?
var transactions = [];
transactions.push({'date': $("date").value, 'type': $("transType").value, 'amount': parseFloat( $("amount").value)});
var i = 0;
alert('date ' + transactions[i].date + ' type ' + transactions[i].type + ' amount ' + transactions[i].amount);

Categories

Resources