Hi gits
thanks for your reply. I am sorry about the link, I didn't think that way. I will be more careful from now.
The function as it stands now, is still giving me issues, and I need a way to reload a given array. I was wondering if you could help.
Here is the situation, the image below is of two examples of a given array, say array_1, when I call a function.
I need a way to reload this array, on function call, in other words, I need to delete unwanted entries.
Here is another array (say array_2) that I have created for that purpose, called with the same function.
The idea is, if array_1 is represented as ex. 1 while calling the function, array_1 should become -
'0'=>"41~0"
'1'=>"43~0"
'2'=>"45~0"
and if array_1 is represented as ex. 2 while calling the function, array_1 should become -
'0'=>"41~0"
'1'=>"43~2"
'2'=>"45~2"
ie, keep whatever is after the ~ or replace by 0 in case there is none, match the first part of value, and delete the rest where the first part is not a part of array_2. Given that, the values of array_1 and array_2 may or maynot be in the same order. Or, the values of array_2 can be hidden without any order in array_1, or may be totally absent, as in ex. 1
Do you have anything to propose me here?
Thanks again.
7 2697 gits 5,390
Expert Mod 4TB
hi ...
do you mean something like this: -
var array_1 = [ '11~0', '56~0', '57~2', '58~1' ];
-
var look_up = { 0: 41, 1: 43, 2: 45 };
-
-
function lookup_list(arr, map) {
-
var a = [];
-
-
for (var i in map) {
-
var temp = arr[i].match(/[^\~]+$/);
-
a.push(map[i] + '~' + temp);
-
}
-
-
return a;
-
}
-
-
var b = lookup_list(array_1, look_up);
-
kind regards
Hi gits
Honestly speaking my javascript is not so develop to understand what you wrote, but I will try with this example now, will let you know then ... thanks :)
gits 5,390
Expert Mod 4TB
Hi gits
Honestly speaking my javascript is not so develop to understand what you wrote, but I will try with this example now, will let you know then ... thanks :)
ok ... let me comment it a bit: -
// one of your arrays
-
var array_1 = [ '11~0', '56~0', '57~2', '58~1' ];
-
-
// instead of an array we use a hashmap here (js-object)
-
// to iterate and store the values to add - represents your
-
// second array
-
var look_up = { 0: 41, 1: 43, 2: 45 };
-
-
// declare a function that compares array_1 and the map
-
function lookup_list(arr, map) {
-
// literally declare an array ... always prefer this instead
-
// of using new Array
-
var a = [];
-
-
// iterate through the map - i are the keys
-
for (var i in map) {
-
// use a regExp to match from the end until
-
// a '~' is found
-
var temp = arr[i].match(/[^\~]+$/);
-
-
// add the value to our new array a
-
a.push(map[i] + '~' + temp);
-
}
-
-
// return the new array
-
return a;
-
}
-
-
// b gets the new array from our function assigned
-
var b = lookup_list(array_1, look_up);
-
kind regards
Hi Gits
Must tell, you are a real genious ... :)
Yeah, it's working as it should ... I'm really surprized the way you manged to do that with such a small function. I thought it would be much longer. And thanks for your explanation. Also, I didn't know it's better to declare a js array with [] instead of doing that with new. Something new I learned :)
I think the heart of that full code is this line - var temp = arr[i].match(/[^\~]+$/);
It's highly complex, conceptually, I must admit.
Thanks again genious :)
gits 5,390
Expert Mod 4TB
we have to do a little adaption ... since it could be that you would have a key in the list that wouldn't have a corresponding key in the array: -
var array_1 = [ '11~0', '56~0', '57~2', '58~1' ];
-
var look_up = { 0: 41, 1: 43, 2: 45 };
-
-
function lookup_list(arr, map) {
-
var a = [];
-
-
for (var i in map) {
-
if (typeof arr[i] != 'undefined') {
-
var temp = arr[i].match(/[^\~]+$/);
-
a.push(map[i] + '~' + temp);
-
}
-
}
-
-
return a;
-
}
-
-
var b = lookup_list(array_1, look_up);
-
next thing would be: is there always a '~' followed by a value in your array?
kind regards
Thanks for the update gits. Yes, actually that array_1 is a id~qnty pair (first id of the item to be selected, and then the quantity taken, starting from zero), so there's always the ~ sign, to seperate the two.
Idea is, user chooses a product for a particular category, choose number of that product, and then move to the next category. Problem that I was facing, was that when the user was moving to the next category, I was calling the same array, and it was already populated from before. So, I had to get rid of previous elements, in case the ids correspond to a different category, or keep it if it's from the same category.
OK, I'm updating my code.
Thanks again. :)
gits 5,390
Expert Mod 4TB
ok ... so the snippet should do the job well now ;) ... post back in case you encounter problems with it ...
kind regards
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jeffrey Silverman |
last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree
structure from that list.
Basically, I want to turn this:
$list = array(
array( 'id' => 'A', 'parent_id' => null, 'value'...
|
by: Christian Seberino |
last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python?
Python's design is godly. I'm wondering if Ruby's is godly too.
I've heard it has solid OOP design but then...
|
by: Kenneth Baltrinic |
last post by:
I am trying to compare values coming out of a database record with known
default values. The defaults are in an array of type object (because they
can be of any basic data type, I am not working...
|
by: Jason |
last post by:
Is there a mechanism in VB.NET that allows something like:
If myVar In ("A","B","C") Then...
The way I'm doing it now is:
Select Case myVar
Case "A","B","C"
Or like this:
|
by: vadymus |
last post by:
Hello,
I need to compare 2 id names but not their values. For example:
document.getElementById(i) == document.getElementById(j)
these elements above seem to give error or do not work.
My...
|
by: Jmc |
last post by:
Hi
Need some advice on how to get all colors in an image.
I wish to have an input file, in my case it will be a scanned piece of
fabric.
I would like to first of all gen an array with all the...
|
by: Assimalyst |
last post by:
Hi,
I have a working script that converts a dd/mm/yyyy text box date entry
to yyyy/mm/dd and compares it to the current date, giving an error
through an asp.net custom validator, it is as...
|
by: =?Utf-8?B?UmljY2FyZG8=?= |
last post by:
What is the best method to compare 2 array to knw if each element is equal?
Now, i have to compare the datarow.itemarray of 2 datarows wich is equals in
structure.
I tried to write this routine:...
|
by: raylopez99 |
last post by:
This is an example of using multiple comparison criteria for IComparer/
Compare/CompareTo for List<and Array.
Adapted from David Hayden's tutorial found on the net, but he used
ArrayList so the...
|
by: toadstool |
last post by:
I have an array containing the various products
$inventory = array();
$inventory = "whatever1";
etc
I have an array containing various values
$quantity = array(100,200,300);
I call in a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |