473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

javascript array headache

M D
How can make the following program so that
remove not matching fruits from a1 and add rest in the a2 to
a1.
answer
["Apple","Grape","Pear","Water Melon", "Straberry"];
currently i get the first 3 because concat won't alocate the new
memory space for a1[4] a1[5]

var a1 = ["Apple","Banana","Orange","Grape","Pear"];
var a2 = ["Grape","Pear","Apple","Water Melon", "Straberry"];
var found;

for( var j = 0; j < a1.length; j++ ){
found = "false";
for( var i = 0; i < a2.length; i++ ){
if( a1[ j ] == a2[i] ){
found = "true";
a2.splice(i,1);
i--;
break;
}else{

}
}

if( found == "false" ){
a1.splice(j,1);
j--;
}
}
alert( a1.length );
alert( a2.length );
a1.concat( a2 );
alert( a1.length );
for( var l = 0; l < a1.length ; l++ ){
alert( a1[l] );
}
Sep 12 '06 #1
1 1125
M D wrote:
How can make the following program so that
remove not matching fruits from a1 and add rest in the a2 to
a1.
[ snip code]
// one Conditio: no double (repeated) items inside a1 or a2
var a1 = ["Apple","Banana","Orange","Grape","Pear"]
var a2 = ["Grape","Pear","Apple","Water Melon","Straberry"]
var c = a1.concat(a2)
var fruit = new Object()
var Y = new Array()
var N = new Array()

for(var i=0; i<c.length; ++i) {
fruit[c[i]]?++fruit[c[i]]:fruit[c[i]]=1
}

for (var j in fruit) {
fruit[j]>1?Y.push(j):N.push(j)
}

alert('matching: ' + Y + '\n' + 'non matching: ' + N)

Hope this helps,

--
Bart

Sep 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: point | last post by:
Hi there.. I have the folowing array => property = hexonet => property = 2003-12-01 18:46:20.0 => property = hexonet => property = 2003-12-02 02:59:15.0 => property = hexonet =>...
4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
1
by: akg250978 | last post by:
ok here is my problem i created the JS to insert rows in an html doc, this works perfectly but if i was to refresh the page or leave it and return to it L8 the rows have diappeared here is the...
9
by: Boris Yeltsin | last post by:
I use Master Pages, so I make use of URL rebasing through the ~ operator, like this in the <head>: <link runat="server" href="~/Root.master.css" media="screen" rel="stylesheet" type="text/css" />...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
7
gchq
by: gchq | last post by:
Hi there Here is the situation - a table is built dynamically with values in the cells I need to retrieve and enter into a database. I have found a way of getting values using JavaScript, but of...
1
tekninja
by: tekninja | last post by:
I wanted to write a function to take the tedium out of dynamically determining the total number of elements in an array. Within the main method this is easy as I simply use: sizeof( <array name>...
15
by: Sampat | last post by:
Hi, I wanted to know the performance of calling a function pointer v/s a normal function call in javascript in a scenario where there are multiple calls in the js to the same function. Please...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.