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 2716 gits 5,390
Recognized Expert Moderator Expert
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
Recognized Expert Moderator Expert
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
Recognized Expert Moderator Expert
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
Recognized Expert Moderator Expert
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' => 'aaa')
, array( 'id' => 'B', 'parent_id' => 'A', 'value' => 'bbb')
, array( 'id' => 'C', 'parent_id' => 'B', 'value' => 'ccc')
, array( 'id' => 'D', 'parent_id' => 'A', 'value' => 'ddd')
|
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 I've also heard there are
lots of weird ways to do some things kinda like Perl which is bad for me.
Any other ideas?
|
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 with weird stuff, just
strings, int, bools and DataTime values) My fields values for this record,
for convenience are also in an array of objects. Now I am trying to write
code like the following.
private void processData (object d, object a)...
|
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 original statement is this (and note, when I add line above to it,
that does not work):
function uniqueValues(){
| |
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 colors
represented in the image.
and an percentage of how much the color exists.
Lets assume that the color of the fabric is blue, it will probably have
pixels that are not #0000FF since the diferent threads in the fabric
|
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 follows:
function doDateCheckNow(source, args)
{
var oDate = document.getElementById(source.controltovalidate); //
dd/mm/yyyy
|
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: (OPTION STRICT MUST BE ON!!)
Private Function ISArrayEquals(ByVal A As Array, ByVal B As Array) As
Boolean
For index As Integer = 0 To A.Length - 1
If A.GetValue(index) <B.GetValue(index) Then '<----Here is the error!
Return False
End If
|
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 format was different.
Basically you can sort a class having members LastName (string),
FirstName (string) and Age (int) according to whether you want to sort
by Last Name, First Name or Age, using the .Sort function
|
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 value from a form $_POST; say
I assign a variable
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |