Page 1 of 1

using where command during mapping from local storage

Posted: Sun Aug 23, 2015 9:08 pm
by Vinny B

Hello

Code: Select all

i am mapping an array from localstorage to a grid. I want to use the where clause is it possible? can u guide me?

using where command during mapping from local storage

Posted: Mon Aug 24, 2015 3:12 am
by M&M

yup, its possible. Can I see a sample json string / object?

One of the ways would be to do something like this and then use this result in echo service

code
// We are taking the text from an input box
var searchTerm = Apperyio('inp').val();

var searchIn = [{"model":"LM3000","brand":"Sony"},{"model":"DX8580","brand":"Nikon"},{"model":"L50","brand":"Yashica"},{"model":"L80","brand":"Yashica"}];

// We will use grep which finds the elements of an array which satisfy a filter function. We can put in complex regex if needed. Here we are doing just a simple trim and lowercase
var filtered_result = $.grep(searchIn, function( myobj, i ) {
return myobj.brand.trim().toLowerCase() === searchTerm.trim().toLowerCase();
});

// Returns an object.
console.log('Type of result: ' + typeof filtered_result);

// Use JSON.stringify to get a JSON String
console.log('Stringified Result: ' + JSON.stringify(filtered_result));
/code


using where command during mapping from local storage

Posted: Tue Aug 25, 2015 2:08 pm
by Vinny B

Wow this works great.. BUT I need to search if the string contains something.

For example: "model" that contains "dx"?

this is my current code
// We are taking the text from an input box
//var searchTerm = Apperyio('inp').val();

var currecord = localStorage.getItem("fireCalendarArray");
arrayLocal = JSON.parse(currecord);

var searchTerm = "2015";
//var search = new RegExp(searchTerm , "i");

var searchIn = arrayLocal;

// We will use grep which finds the elements of an array which satisfy a filter function. We can put in complex regex if needed. Here we are doing just a simple trim and lowercase
var filtered_result = $.grep(searchIn, function( myobj, i ) {
return myobj.Month.trim().toLowerCase() === searchTerm.trim().toLowerCase();

});

// Returns an object.
console.log('Type of result: ' + typeof filtered_result);

// Use JSON.stringify to get a JSON String
console.log('Stringified Result: ' + JSON.stringify(filtered_result));


using where command during mapping from local storage

Posted: Tue Aug 25, 2015 3:34 pm
by M&M

that's pretty straight forward. You can use javascript function indexOf

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value.
returns -1 if the value is not found.

code
var modelname = "x-dx100&quot
alert(modelname.indexOf("dx") > -1); // Will return you true / 2. If it's minus 1 that means the string has not been found

'Pretty Girl'.indexOf('Pretty' [, Optional fromIndex]); // returns 0
/code

Use the indexOf method in the grep to return an array that has the search string


using where command during mapping from local storage

Posted: Tue Aug 25, 2015 3:48 pm
by Vinny B

im sorry but i have no idea what that means, I get a two digit alert.

In your code how would you search model that had a D in it. the result would be:

Result
model DX8580

// We are taking the text from an input box
var searchTerm = Apperyio('inp').val();

var searchIn = [{"model":"LM3000","brand":"Sony"},{"model":"DX8580","brand":"Nikon"},{"model":"L50","brand":"Yashica"},{"model":"L80","brand":"Yashica"}];

// We will use grep which finds the elements of an array which satisfy a filter function. We can put in complex regex if needed. Here we are doing just a simple trim and lowercase
var filtered_result = $.grep(searchIn, function( myobj, i ) {
return myobj.brand.trim().toLowerCase() === searchTerm.trim().toLowerCase();
});

// Returns an object.
console.log('Type of result: ' + typeof filtered_result);

// Use JSON.stringify to get a JSON String
console.log('Stringified Result: ' + JSON.stringify(filtered_result));:


using where command during mapping from local storage

Posted: Tue Aug 25, 2015 4:49 pm
by M&M

// We are taking the text from an input box
var searchTerm = Apperyio('inp').val().trim().toLowerCase();
var strModel = "";

var searchIn = [{
"model": "LM3000",
"brand": "Sony"
}, {
"model": "DX8580",
"brand": "Nikon"
}, {
"model": "L50",
"brand": "Yashica"
}, {
"model": "L80",
"brand": "Yashica"
}];

// We will use grep which finds the elements of an array which satisfy a filter function. We can put in complex regex if needed. Here we are doing just a simple trim and lowercase
var filtered_result = $.grep(searchIn, function(myobj, i) {
strModel = myobj.brand.trim().toLowerCase();
return (strModel.indexOf(searchTerm) -1);
});

// Use JSON.stringify to get a JSON String. Use this for your echo service
console.log('Stringified Result: ' + JSON.stringify(filtered_result));


using where command during mapping from local storage

Posted: Tue Aug 25, 2015 5:02 pm
by M&M

I have changed my original code to filter based on the search term in the model

code
// We are taking the text from an input box
var searchTerm = Apperyio('inp').val().trim().toLowerCase();

var productsList = [{
"model": "LM3000",
"brand": "Sony"
}, {
"model": "DX8580",
"brand": "Nikon"
}, {
"model": "L50",
"brand": "Yashica"
}, {
"model": "L80",
"brand": "Yashica"
}];

// We will use grep which finds the elements of an array which satisfy a filter function. We can put in complex regex if needed. Here we are doing just a simple trim and lowercase
var filtered_result = $.grep(productsList, function(obj, i) {
return (obj.model.trim().toLowerCase().indexOf(searchTerm) > -1);
});

// Use JSON.stringify to get a JSON String. Use this for your echo service
console.log('Stringified Result: ' + JSON.stringify(filtered_result));
/code


using where command during mapping from local storage

Posted: Tue Aug 25, 2015 6:59 pm
by Vinny B

I truly appreciate your help and I have everything I need working with the exception of how to map it to the grid or where to put it in the app. Maybe Ill start another thread for that?

I replaced console.log with

var out = JSON.stringify(filtered_result).split(",");
var map = Appery("month_grid").text(out);
return map;

I also tried dozens of other options. I have been working on this for a many many many hours.