Pages

Friday 24 July 2015

AlaSQL & How to find index of an item in JavaScript Object Array?

AlaSQL

We focus on speed by taking advantage of the dynamic nature of javascript when building up queries. Real world solutions demands flexibility regarding where data comes from and where it is to be stored. We focus on flexibility by making sure you can import/export and query directly on data stored in your own JSON object, Excel files, localStorage, IndexedDB, and SQLite.

How to use

For the browser: Include alasql.min.js and call alasql() with your SQL statements:
<script src="//cdn.jsdelivr.net/alasql/0.1/alasql.min.js"></script> 

<script>

    alasql("CREATE TABLE cities (city string, population number)");

    alasql("INSERT INTO cities VALUES ('Rome',2863223), ('Paris',2249975), ('Berlin',3517424),  ('Madrid',3041579)");

    var res = alasql("SELECT * FROM cities WHERE population < 3500000 ORDER BY population DESC");

   // res now contains this array of object:
   // [{"city":"Madrid","population":3041579},{"city":"Rome","population":2863223},{"city":"Paris","population":2249975}]   

</script>
Example:
var data = [ 
    { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
    { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
    { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
    { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
    { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
    { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
    { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
    { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
]
    var res = alasql('SELECT Phase,  SUM(CAST([Value] AS INT)) AS [Value] FROM ? GROUP BY Phase',[data]);
document.getElementById("res").textContent = JSON.stringify(res);
<span id="res"></span>
Output:
[{"Phase":"Phase 1","Value":50},{"Phase":"Phase 2","Value":130}]
Play with this example in jsFiddle

How to find index of an item in JavaScript Object Array?

Recently while working I came across a scenario. I had to find index of a particular item on given condition from a JavaScript object array. In this post we will see how to find index of object from JavaScript array of object.
Let us assume we have a JavaScript array as following,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var studentsArray =
[
{
"rollnumber": 1,
"name": "dj",
"subject": "physics"
},
{
"rollnumber": 2,
"name": "tanmay",
"subject": "biology"
},
{
"rollnumber": 3,
"name": "amit",
"subject": "chemistry"
},
];
Now if we have a requirement to select a particular object in the array. Let us assume that we want to find index of student with name Tanmay.
We can do that by iterating through the array and comparing value at the given key.
1
2
3
4
5
6
7
8
9
10
function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}
You can use the function to find index of a particular element as below,
1
2
var index = functiontofindIndexByKeyValue(studentsArray, "name", "tanmay");
alert(index);
In this way you can find index of an element in JavaScript array of object. I hope you find this quick post useful. Thanks for reading.

No comments:

Post a Comment